-->
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.  [ 4 posts ] 
Author Message
 Post subject: hibernate nested update and create
PostPosted: Fri May 20, 2005 6:12 am 
Newbie

Joined: Fri May 20, 2005 6:05 am
Posts: 8
I am using Hibernate 3.0.4 and trying to update an object that has a new subobject and that subobject has objects as well.. For example

foo has a one to many with bar which has a one to many with junk.

the save fails because bars generated id never gets set in the junk objects so when junk tries to save it gets a foriegn key violation.

is this kind of transaction supported in hibernate or do I have to save the subobjects first.

John


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 11:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You need to read the documentation! Specifically you are looking for cascade settings.


Top
 Profile  
 
 Post subject: I read the docs
PostPosted: Fri May 20, 2005 2:32 pm 
Newbie

Joined: Fri May 20, 2005 6:05 am
Posts: 8
I have read the documentation..

I set cascade to "save-update,delete". A quick sample would be

public class A {
private long aId;
private Set bs;
public long getAId() {return aId;}
public void setAId(long aId){this.aId= aId;}
public set getBs() {return bs;}
public void setBs(Set bs){this.bs = bs;}
}

public class B {
private long bId;
private Set bs;
private long aId;
public long getBId() {return bId;}
public void setBId(long bId){this.bId = bId;}
public set getCs() {return cs;}
public void setCs(Set bs){this.bs = cs;}
public long getAId() {return aId;}
public void setAId(long aId){this.aId= aId;}
}

public class C {
private long cId;
private long bId;
public long getCId() {return cId;}
public void setCId(long cId){this.cId= cId;}
public long getBId() {return bId;}
public void setBId(long bId){this.bId = bId;}
}


if I do

A a = new A();
Set bs = new Set(); // yes I know Set is not right but for demo only
B b = new B();
bs.add(b);
Set cs = new Set();
C c = new C();
cs.add(c);
bs.setCs(cs);
a.setBs(bs);

session.saveOrUpdate(a);

the insert on b fails because b.aId does not get set.

I have tried to map to the id and put the Object A there and neither worked.

John


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 2:42 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Go read some more on bidirectional associations and inverse setttings.


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