-->
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.  [ 5 posts ] 
Author Message
 Post subject: Limitations of inverse attribute...
PostPosted: Mon Jul 25, 2005 10:45 am 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
Hello,

I'm using a many-to-one relationship with the inverse attribute on the many side.

Since it is not possible to set the attribute true to the one side how can I make Hibernate insert the collection from the many side and not the one side.

Indeed, my problem is similar to the example of Hibernate in Action on page 109. But when I insert (corresponding to the example) the Item in the DB I would like Hibernate by cascade insert automatically the collection Bids because once the Item is persistent, the collection bids won't change.

Presently, if I insert the Item in the database, the collection obviously won't be inserted because of the inverse="true" which means I have to iterate on the collection and to insert a bid one by one.

I hope I'm clear.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 3:45 pm 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
The parameter inverse="true" only affects who writes the column that maps the relationship. You can use cascade="save-update" in the one side and the collection will be saved automatically.


Here is an example:
Code:
    <class name="Father" table="father">
        <id name="id" column="id">
            <generator class="sequence" />
        </id>
        <set name="children" cascade="all" inverse="true">
            <key column="father_id" not-null="true"/>
            <one-to-many class="Child"/>
        </set>
    </class>

    <class name="Child" table="child">
        <id name="id" column="id">
            <generator class="sequence" />
        </id>
        <many-to-one name="father" class="Father" column="father_id"/>
    </class>


When you save an object of class Father, Hibernate cascades the save to the children collection and saves all the Child objects linked to it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 5:37 am 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
Thank you for your message.

I did set the cascade attribute to save-update but when insering the Father and processing with the children, I got a message informing me that the collection was [<unreferenced>].

As you said, the inverse attribute affects who writes the column but if this attribute is set to "true" it means the Father won't write the column even if the cascade attribute is set to save-update which is my case.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 6:06 am 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
dprotas, you were right.

It did not work because I had set the default-cascade attribute to true and not the specific cascade attribute of the collection.

However, I still can't understand the inverse attribute.
In the book "Hibernate in Action" page 109, it is said that "if we only call item.getBids().add(bid), no changes will be persistent".

In our case, it's
Code:
father.getChildren.add(child)
.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 6:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Put away the book. Open the Hibernate reference documentation. There is a chapter called "Parent Child Example". Do this step-by-step without changing the code or doing anything else. After you have done this, go to the Hibernate website and search for "inverse". Open the first page in the result and read it.


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