-->
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: bi directional one-to-many child's parent_id not getting set
PostPosted: Tue Oct 19, 2010 1:28 am 
Newbie

Joined: Tue Oct 19, 2010 1:26 am
Posts: 3
All-

I have this recent problem that I cannot seem to wrap my head around and - in six years of using hibernate haven't ever run into...

I have a bi-directional relationship between Union 1--∞ Chapter. When I create a Union object, instantiate the set (or list, even), create a new chapter and set that chapter on the set -- I get an exception where the UNION_ID on the Chapter object is null. I've been spinning my wheels for hours trying to determine what's going on. The relevant portion of the mapping is below...

... Union {
@OneToMany(mappedBy = "union", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Chapter> chapters;
}

... Chapter {
@ManyToOne
@JoinColumn(name = "UNION_ID", nullable = false)
private Union union;
}

... and, of course, the relevant, public getters and setters

... execution code looks more or less like...

Union myUnion = new Union();
Chapter myChapter = new Chapter();
Set<Chapter> chapters = new HashSet<Chapter>();
chapters.add(myChapter);

unionService.persist(myUnion);

...

At this point, the transaction fails because "UNION_ID" is NULL for the chapter insert, violating the schema.

Any thoughts???


Top
 Profile  
 
 Post subject: Re: bi directional one-to-many child's parent_id not getting set
PostPosted: Tue Oct 19, 2010 2:58 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You are missing at least two lines of code:

Code:
myUnion.setChapters(chapters);
myChapter.setUnion(myUnion);


Top
 Profile  
 
 Post subject: Re: bi directional one-to-many child's parent_id not getting set
PostPosted: Tue Oct 19, 2010 7:19 am 
Newbie

Joined: Tue Oct 19, 2010 1:26 am
Posts: 3
Ack. Yeah, I forgot to add that line to the post. So I am setting the chapters on the Union object.

I've _never_ had to manually set the parent on the child. Simply adding the child to a parent's List or Set member annotated as a @OneToMany did the trick.

Union myUnion = new Union();

Set<Chapter> chapters = new HashSet<Chapter>();
Chapter myChapter = new Chapter();
chapters.add(myChapter);

---> FORGOT LINE ---> myUnion.setChapters(chapters);

unionService.persist(myUnion);

---

So when did it become a requirement that you have to manage both sides of the relationship?


Top
 Profile  
 
 Post subject: Re: bi directional one-to-many child's parent_id not getting set
PostPosted: Tue Oct 19, 2010 7:35 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
So when did it become a requirement that you have to manage both sides of the relationship?


As far as I know it has always been required that the owning side of the association is properly set. In this case it is the Chapter that owns the link, so myChapter.setUnion() must be called.


Top
 Profile  
 
 Post subject: Re: bi directional one-to-many child's parent_id not getting set
PostPosted: Tue Oct 19, 2010 7:37 am 
Newbie

Joined: Tue Oct 19, 2010 1:26 am
Posts: 3
So... There are two things that are different about this project; (a) I'm using spring mvc 3.0 instead of struts 2.1.x and (b) I'm using JPA 2 instead of pure hibernate (I think the last project was 3.3). I'm starting to get the feeling that maybe Struts was setting the relationship for me? -- especially when the type is a list. In my form I'd have something like...

Code:
<input ... name="union.someProperty" value="..." />
<input ... name="union.chapters.name" value="..." />
<input ... name="union.chapters.description" value="..." />


... which would automagically instantiate the concrete list on the Union, create ONE chapter object ... and set the union on the chapter? Then in the struts action you have a getter for "union", type Union, which would be the object all rolled up.


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.