-->
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.  [ 3 posts ] 
Author Message
 Post subject: Parent-Child Association
PostPosted: Tue Apr 13, 2004 5:14 am 
Newbie

Joined: Mon Feb 23, 2004 8:30 am
Posts: 13
Location: Istanbul, Turkey
Hello,

I was implementing a parent-child relationship with map. But there was a problem. The index of the map was not stored in the database.

I obeyed the rules explained in the Chapter 9. Parent/Child Relationships of the Hibernate2 Documentation.

The documentation (9.2) says that the relationship between the parent/child should be specified as inverse="true" inside the parent's mapping.

If this is not specified then there will be an INSERT to create the child and an UPDATE to create the link from parent to child. So INSERT will violate the not-null constraint on the parent_id column.

But when I made inverse="false" these problems didn't occur. Moreover the former problem was resolved, namely the index of the map is stored correctly.

My code is:

Code:
        Parent p = (Parent) s.load(Parent.class,pid);
        Child c = new Child();
        p.addChild(c);
        s.save(c);
        s.flush();


Mapping of the parent's map is:
Code:
      <map name="children" table="children" lazy="false" >
         <key column="parentId" />
         <index column="i" type="integer" />
         <one-to-many class="Child" />
      </map>


and the association from child to parent:
Code:
        <many-to-one name="parent" class="Parent" not-null="true" column="parentId" />


According to the documentation, the above code should fail. But it works correctly. What is the reason, may anybody help me to understand this?

_________________
Mert Nuhoglu
http://jroller.com/page/mnuhoglu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 7:22 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can't used indexed collections or maps with a bidirectional one-to-many association. Either use a Set, a Bag, or make it unidirectional.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Understood lastly :)
PostPosted: Fri Apr 16, 2004 3:48 am 
Newbie

Joined: Mon Feb 23, 2004 8:30 am
Posts: 13
Location: Istanbul, Turkey
Thank you Christian, at first I couldn't give a meaning to what you said. I thought my corrupt solution was working correctly. But then I saw other mistakes in the solution...

I write here the Parent -> Child sample implementation for other people, who encounter the same problem.

The Child class (getter/setters omitted):

Code:
public class Child {
    Integer id;


The Parent class: (Note that Parent sees Child but Child cannot see Parent)

Code:
public class Parent {
    private Map children;
    private Integer id;
    private int lastPosition ;
    public void addChild(Child child){
        lastPosition += 10;
        getChildren().put(new Integer(lastPosition),child);
    }


Parent's mapping:

Code:
   <class name="Parent" table="Parent">
      <id name="id" type="integer" column="id">
         <generator class="native"/>
      </id>
        <property name="lastPosition" column="lastPosition" />
      <map name="children" table="children" lazy="false" cascade="all"  >
         <key column="parentId" />
         <index column="i" type="integer" />
         <one-to-many class="Child" />
      </map>
   </class>


And the test method:

Code:
    public void testMapSavingCascaded() throws HibernateException {
        Serializable pid = new Integer(1);
        Parent p = (Parent) s.load(Parent.class,pid);
        Transaction tx = s.beginTransaction();
        Child c = new Child();
        p.addChild(c);
        tx.commit();
        s.close();
        s = sf.openSession();
        Parent result = (Parent) s.load(Parent.class,pid);
        assertEquals("Parent is not saved",p,result);
    }

_________________
Mert Nuhoglu
http://jroller.com/page/mnuhoglu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.