-->
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: Force insert on persistent object
PostPosted: Wed Jan 04, 2006 7:03 am 
Newbie

Joined: Mon Nov 28, 2005 2:35 pm
Posts: 14
Hi!

I have one object, which I want to insert twice into the same table.
This is requiered for keeping a history.
The inserts should happen within in the same transaction.
And look something like
Code:
...
session = HibernateUtil.currentSession();
transaction = session.beginTransaction();
TObject o = new TObject();
....
session.save(o);
o.setValue(v);
session.save(o);
transaction.commit();
...
finally{
session.close();
}

Unfortunately Hibernate thinks, that it has to perform an UPDATE on the second time calling session.save(o) and does not insert o again. These
seems to make session.update and session.saveOrUpdate redundant.

In a second try I have tried to clone o and then save the clone, but with the same result. Who can help?

thx in advance[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 10:11 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Check your equals() method of TObject.
Does it take the value you passed with setValue(v) into account?
Do you have an id property?
What is your primary key on the table?

Read:
http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html

Urs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 10:37 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Once you saved your object it is in the persistant state (as mentioned by urs_b read the reference here).

You cannot change the primary key of an object in this state as long it is connected to a session.
So your choice is
a)
close session -> change primary key -> save
which I do not recommend
b)
make a copy of your object (not including the primary key of course)
which is a viable solution
c)
work with timestamps -> read about versioning in the manual

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: Sun Jan 08, 2006 6:26 pm 
Newbie

Joined: Mon Nov 28, 2005 2:35 pm
Posts: 14
Thx for your help. Thanks to your clues I was able to solve the problem.


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.