-->
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: Confused in the session & trancation
PostPosted: Wed Jan 07, 2004 10:56 pm 
Newbie

Joined: Tue Jan 06, 2004 7:09 am
Posts: 2
Mapping file
Code:
  <class name="MyBean.Child" table="Child">
    <id name="id" column="id" type="java.lang.Long">
      <generator class="native"/>
    </id>
    <many-to-one name="Parent" column="parent_id" cascade="save-update"/>
  </class>

  <class name="MyBean.Parent" table="Parent">
    <id name="id" column="id" type="java.lang.Long">
      <generator class="native"/>
    </id>
    <set name="children" cascade="save-update">
      <key column="parent_id"/>
      <one-to-many class="MyBean.Child"/>
    </set>
  </class>





Java code
Code:
        //Save single parent,child

        {
            Parent parent = new Parent();
            Child child = new Child();
            s.save(parent);
            s.save(child);
            s.flush();
            t.commit();
            System.out.println("End Save single parent,child");
        }

        //Save parenty by saving child
        {
            Child child = new Child();
            Parent parent = new Parent();
            child.setParent(parent);
            s.save(child);
            t.commit();
            System.out.println("End Save parent by saving child");
        }

        //Load a parent and add a child
        {
            Parent parent = (MyBean.Parent)s.load(MyBean.Parent.class,new Long(2));
            Child child = new Child();
            child.setParent(parent);
            parent.getChildren().add(child);
            s.update(parent);
            t.commit();
            System.out.println("End load a parent and add a child");
            s.flush();
        }

        //Dump the children
        {
            Parent parent = (MyBean.Parent)s.load(MyBean.Parent.class,new Long(2));
            Set children = parent.getChildren();
            System.out.println(children.size());
            //System.out.println( ((MyBean.Child) children.iterator().next()).getId() );
            Iterator iterator = children.iterator();
            while(iterator.hasNext())
            {
                Child child = (MyBean.Child) iterator.next();
                System.out.println( "Child:" + child.getId() );
            }
        }


All of the upper codes run smoothly.but when it execute the process named "Dump the children".

It would return the children which was added at the process "Save parenty by saving child" and miss the child added at prcoess "Load a parent and add a child"

I go to the database for searching.There are 2 children matching the parent(id=2).

Why?

If I comment all of the process excep the "Dump the children",and restart the program.It can correctly dump 2 children.

I very confused at the session & transaction.
Does the Hibernate cache the parent object?
or we must close the session first before reload the parent?

Any idea appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 7:21 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes Hibernate cache the parent object.
Tx opening is required even for DB reading.
Why do you commit several time wo restarting any tx ?

I read in you code
commit();
flush();

Read the doc about commit() and flush(). A commit will flush if the Tx is an hibernate one. Otherwise, the flush is expected before.

_________________
Emmanuel


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.