Hibernate version: 2.1.6
Mapping documents: <class name="test.Child" table="child" lazy="true">
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" />
<many-to-one name="parent" column="parent_id"/>
</class>
<class name="test.Parent" table="parent" lazy="true">
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" />
<set name="children" inverse="true" lazy = "true" cascade="all">
<key column="parent_id"/>
<one-to-many class="test.Child"/>
</set>
</class>
Code between sessionFactory.openSession() and session.close():Code:
//add parent
Session ss = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
Parent p = new Parent();
p.setName("parent");
ss.save(p);
//add a child. only set the child's parent
System.out.println("Add a child. Only set the child's parent......");
Child c1 = new Child();
c1.setName("child_Set-One-End");
c1.setParent(p);
ss.save(c1);
//add a child. set the child's parent and add the child to the parent
System.out.println("Add a child. Set the child's parent and add the child to the parent......");
Child c2 = new Child();
c2.setName("child_Set-Two-End");
c2.setParent(p);
[b]p.getChildren().add(c2);[/b]
ss.save(c2);
HibernateUtil.commitTransaction();
System.out.println("Query the parent's children......");
//before sesion closed
System.out.println("Before session closed......");
List result_before = ss.createCriteria(Parent.class).add(
Expression.eq("name", "parent")).list();
for (Iterator it = result_before.iterator(); it.hasNext();) {
Parent p_query = (Parent) it.next();
for (Iterator it2 = p_query.getChildren().iterator(); it2
.hasNext();) {
System.out.println(it2.next().toString());
}
}
//after sesion closed
System.out.println("After session closed......");
[b]HibernateUtil.closeSession();[/b]
ss = HibernateUtil.getSession();
List result_after = ss.createCriteria(Parent.class).add(
Expression.eq("name", "parent")).list();
for (Iterator it = result_after.iterator(); it.hasNext();) {
Parent p_query = (Parent) it.next();
for (Iterator it2 = p_query.getChildren().iterator(); it2
.hasNext();) {
System.out.println(it2.next().toString());
}
}
HibernateUtil.closeSession();
Result:Add a child. Only set the child's parent......
Add a child. Set the child's parent and add the child to the parent......
Query the parent's children......
Before session closed......
child_Set-Two-End
After session closed......
child_Set-One-End
child_Set-Two-End
My Question:Save the first Child (Only set the child's parent) successfully. The data in DB is correct:
child
id | name | parent_id
----+----------------------+-----------
2 | child_Set-One-End | 1
3 | child_Set-Two-End | 1
parent
id | name
----+--------
1 | parent
the query code:
Code:
List result_before = ss.createCriteria(Parent.class).add(
Expression.eq("name", "parent")).list();
for (Iterator it = result_before.iterator(); it.hasNext();) {
Parent p_query = (Parent) it.next();
for (Iterator it2 = p_query.getChildren().iterator(); it2
.hasNext();) {
System.out.println(it2.next().toString());
}
}
But I can't find it by getChildren() before the seesion close. After the session close, it seems OK.Very confused. If set the child's parent and add the child to the parent, everthing is OK. Is this also the problem wiht "inverse = true" ?
In my application,we want to only set one end in an association, and query the children before the seesion closed, What shall we do?