-->
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: Saving new Object without TransientObjectException
PostPosted: Thu Oct 21, 2004 1:02 pm 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
Hibernate version:
2.1.6

I have class A with an attribute of type B, and a setB() method in A. There is a many-to-one relation between A and B. I need to save an instance of A, and I would like to do a setB() with a new object B, not read from the DB (for performance reason).
I know the primary key of B and I've set it, but when I save A, I get a TransientObjectException.
Is there a way to avoid reading B from the DB, without changing the mapping? (This is a tool that do this job on many classes, and I do not know the structure of these classes).
Any suggestion will be greatly appreciated.
Regards
Alessandro Rizzi


Top
 Profile  
 
 Post subject: Re: Saving new Object without TransientObjectException
PostPosted: Wed Sep 21, 2005 6:40 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
I solved the problem in this way

Created a new Object of class B, set the primary key and the attribute mapped as <version ...>. The value for this last attribute is meaningless.
If anyone need to do something like this.


Top
 Profile  
 
 Post subject: Having same problem
PostPosted: Fri Apr 14, 2006 3:17 pm 
Newbie

Joined: Mon Mar 21, 2005 6:55 pm
Posts: 2
I'm having the same problem, can you explain a little more how you resolved your problem using <version ...> In particular, could you share the relevant parts of your mapping files?

Thanks,
Jed


Top
 Profile  
 
 Post subject: Re: Having same problem
PostPosted: Tue Apr 18, 2006 3:31 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
jedfonner wrote:
I'm having the same problem, can you explain a little more how you resolved your problem using <version ...> In particular, could you share the relevant parts of your mapping files?

Thanks,
Jed


I've mapped a field named countUpd as <version> like this:

<version
"column" = "count_upd"
"name" = "countUpd"
"type"= "long"
/>

and then to save the object A I write something like:

b.setPrimaryKey(val);
b.setCountUpd(new Long(0));
a.setB(b);
a.save();

Hope this helps
Bye
Alessandro


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 8:50 pm 
Beginner
Beginner

Joined: Mon Nov 29, 2004 2:26 pm
Posts: 28
If you enable lazy="true" for you class, Hibernate probably won't load the object from the database in this case. So you could do:

a.setB(session.load(123));
session.save(a);


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.