Domanda

In the below given code, cascade="save-update", is used for the Course class(bag) associated with the Student class.

Student class is-->

    private int id;
    private String firstName;
    private String lastName;
    private Address address;
    private List<Course> courses;

Course class is-->

    private int id;
    private String name;
    private int unit;

hbm file for Student is-->

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate.Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="pojos.Student" table="Student">

        <id name="id" type="integer" column="ID">
            <generator class="increment" />
        </id>

        <property name="firstName">
            <column name="FIRST_NAME" />
        </property>

        <property name="lastName">
            <column name="LAST_NAME" />
        </property>

        <many-to-one name="address" 
                     class="pojos.Address" 
                     column="ADDRESS_ID"
                     cascade="save-update" />

        <bag name="courses" inverse="true" cascade="save-update">
            <key column="STUDENT_ID" />
            <one-to-many class="pojos.Course" />
        </bag>

    </class>
</hibernate-mapping>

Transaction I'm performing is simply-->

                    Student stud = new Student("Ketan", "Dikshit");
            Address address = new Address("Dm-Road", "Uttar Pradesh", "201301");
            Course course1 = new Course("Core Java", 101);
            Course course2 = new Course("Advanced Java", 201);

            List<Course> courses = new ArrayList<Course>();
            courses.add(course1);
            courses.add(course2);

            stud.setAddress(address);
            stud.setCourses(courses);

            try {
                tr = session.beginTransaction();
                System.out.println("\n Transaction has begun..!!");
                session.save(stud);
                tr.commit();
                System.out.println("\n Transaction is Commit..!!");
                session.close();
                System.out.println("\n Session is Closed..!!");

            } catch (Exception e) {
                System.out.println("\n Transaction is in errror..!!");
                tr.rollback();
                System.out.println("\n Transaction is RollBack..!!");
                session.close();
                System.out.println("\n Session is Closed..!!");
            }

Now,As per the code, The Courses table should be holding a column named "STUDENT_ID" with the primary key of the associated Student present for each entry in the Course table.But the column "STUDENT_ID" is not showing any data( all null values inserted).

The query sequence is-->

 Transaction has begun..!!
Hibernate: select max(ID) from Student
Hibernate: select max(ID) from Address
Hibernate: select max(ID) from Course
Hibernate: insert into Address (STREET, CITY, ZIPCODE, ID) values (?, ?, ?, ?)
Hibernate: insert into Student (FIRST_NAME, LAST_NAME, ADDRESS_ID, ID) values (?, ?, ?, ?)
Hibernate: insert into Course (NAME, UNIT, ID) values (?, ?, ?)
Hibernate: insert into Course (NAME, UNIT, ID) values (?, ?, ?)

 Transaction is Commit..!!

Why is the column in Course table showing NULL values, instead of holding the id's for the Student??

È stato utile?

Soluzione

Remove inverse="true" from your bag mapping otherwise the owner of this relation will be Course and not Student.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top