-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Question about query the parent / child relationship
PostPosted: Sun Sep 26, 2004 11:43 am 
Newbie

Joined: Sun Sep 26, 2004 8:56 am
Posts: 3
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

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?

My Appliction always


Top
 Profile  
 
 Post subject: Re: Question about query the parent / child relationship
PostPosted: Sun Sep 26, 2004 12:02 pm 
Newbie

Joined: Sun Sep 26, 2004 8:56 am
Posts: 3
qdong wrote:
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?



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.