-->
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.  [ 7 posts ] 
Author Message
 Post subject: Question about persisting child objects (one-to-many)
PostPosted: Wed Apr 05, 2006 5:37 pm 
Newbie

Joined: Wed Apr 05, 2006 5:13 pm
Posts: 4
Hello,

I have a general question and (based on the answer to that question) a specific question about an issue I'm trying to work out.

First question:

When you save a brand new parent object which has a set of child objects (one-to-many collection) do you explicitly have to set the foreign key id of the child to match the generated id of the parent?

Second Question:

If the answer is no, then what am I missing here? When I saveOrUpdate on my parent object, it saves fine, however the generated id (which is the primary key for the parent) does not get set on the child objects (which is a foreign key to the parent's primary key)

My parent object is called Matches and my child object is called Activities

Mapping documents:

Code:
<hibernate-mapping>
    <class name="Match" table="Matches">

        ...

        <set name="matchedActivities" cascade="all-delete-orphan">
            <key column="MatchId"/>
            <one-to-many class="Activity"/>
        </set>
    </class>
</hibernate-mapping>


<hibernate-mapping>
    <class name="Activity" table="MatchedActivities">
        <composite-id name="primaryKey" class="ActivityPrimaryKey">
              <key-property  name="refId" type="java.lang.String" column="RefId"/>
              <key-property  name="refSrc" type="java.lang.String" column="RefSrc"/>
        </composite-id>

        <property 
            name="matchId"
            type="java.lang.Integer"
            update="true"
            insert="true"
            column="MatchId"
        />

    </class>
</hibernate-mapping>



When I call saveOrUpdate, it complains that it cannot save a null value into MatchActivities.MatchId.

Thanks in advance for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 5:41 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Either specify inverse=false on the many side or remove the not null tag.
When you do not inverse, Hibernate will save the child first without primary key set and then update its after having saved the parent.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 5:49 pm 
Newbie

Joined: Wed Apr 05, 2006 5:13 pm
Posts: 4
Can you be more specific? I don't have a "not-null" tag anywhere that I can find.

Also, I had tried inverse="false", but on the 'one' side. where on the 'many' side would I specify this?

Thanks for the quick response!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 6:16 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Sorry, I read to fast over your text.

Do you set both sides of the relation?
a.getBs().add(b)
b.setA(a);

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 11:29 pm 
Newbie

Joined: Wed Apr 05, 2006 5:13 pm
Posts: 4
No, I didn't. Are you supposed to? It seems redudant to me.

Also, why does B need to know about A for the relationship to work? Couldn't you possibly have multiple parents (hierarchy) for an object??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 8:04 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
It is redundant but needed as Hibernate will not scan the memory for objects needing to be updated when you set a relation.

I am not sure, if I got your second question. Java does not support inheritance from multiple parent classes.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 11:09 am 
Newbie

Joined: Wed Apr 05, 2006 5:13 pm
Posts: 4
I understand the fundamentals of java, but with databases the same fundamentals do not apply. I see what you mean, and basically you would need to model a VO hierarchy to persist children with multiple parents.

Thanks for the help.


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