-->
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.  [ 10 posts ] 
Author Message
 Post subject: Parent needs to be saved before child
PostPosted: Mon Feb 16, 2009 12:57 pm 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Hi,

I have the following problem. We have two entities A and B, B have a FK to A (B -> A), so A is the parent in this relationship. In my hbms for A and B I have this:

A.hbm.xml

Code:
<set name="bSet" inverse="true" lazy="true" table="B_TABLE"  cascade="all">
<key column="bColumn" />
<one-to-many class="foo.B" />
</set>   


B.hbm.xml

Code:
<many-to-one name="aEntity" column="aColumn" class="foo.A"
  not-null="true" cascade="none" />   



If i try the next pseudocode, it returns an exception saying (null or transient etc, i haven't the trace here):

Code:
// Code for starting transaction

A aObj= new A();
B bObj= new B();

bObj.setAEntity(aObj);
aObj.setBSet(new HashSet<B>());
aObj.getBSet().add(bObj);

session.saveOrUpdate(aObj);
session.flush();

// Code for finishing transaction


Any help would be appreciated, we are using hibernate 3.0.5

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 12:59 pm 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
I forgot it, the last code throws an exception, so I have to save first the parent and after that i'm able to save the child ....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 2:37 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There is an error in the <set> mapping in A.hbm.xml

Code:
<key column="bColumn" />


shold be:

Code:
<key column="aColumn" />


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 5:36 am 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Hi, you are right, i posted A.hbm.xml with an error, but i have it correct in my real application. What i need to know is if the following code is ok and if it should work. Nowadays it throws an exception about transient, null ... and mentions A object ...


Code:
// Code for starting transaction

A aObj= new A();
B bObj= new B();

bObj.setAEntity(aObj);
aObj.setBSet(new HashSet<B>());
aObj.getBSet().add(bObj);

session.saveOrUpdate(aObj);
session.flush();

// Code for finishing transaction


Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 6:04 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The code seems to be ok, and also the <set> and <many-to-one> mapping. It would be useful to see the complete stacktrace, and maybe also a debug log from Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 7:24 am 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Here is the exception returned, there's no more trace:

Code:
org.springframework.orm.hibernate3.HibernateSystemException: not-null property references a null or transient value: foo.B.aEntity; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: foo.B.aEntity



Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 7:38 am 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Hi, i forgot an important detail in my code, there's a merge ... and it's the merge method who throws the exception (i've already checked this). Can't i invoke the merge for new data to be inserted?


Code:
// Code for starting transaction

A aObj= new A();
B bObj= new B();

bObj.setAEntity(aObj);
aObj.setBSet(new HashSet<B>());
aObj.getBSet().add(bObj);

session.saveOrUpdate(session.merge(aObj));
session.flush();

// Code for finishing transaction



Greets


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 7:41 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
No, merge is used to reattach detached objects. Your objects are transient, it is enough to call saveOrUpdate.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 7:46 am 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Before you tell me... i know the merge is not necessary there, but i use it because is code inside a more generic method ...

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 7:57 am 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Ok, thanks for your replies, it's clear now, greets.


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