Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Save Or Update In Hibernate Always Updating

 
Stuart Houston
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a DTIMER object which i am loading from a table, then i try to save it to another table in a new Database. the primary key(timer_id) will ALWAYS have a value in it, when i use saveOrUpdate it logically always trys to update the record as the unsaved-value is null

How would i tell it to save Or update the record in the new Table without having to null the primary key on the DTIMER object? if i use unsaved type="ANY' it will always insert and thats not what i want.

I cant find much information in the reference manual on the usage of unsaved-value="ID_VALUE" can anyone explain the usage of this?

Hibernate version: 2.1.3

Mapping documents:
<hibernate-mapping>
<class name="DTimer" table="d_timer">
<id name="timerID" column="id" type ="java.lang.Long" unsaved-value="null">
<generator class="native" />
</id>
<property name="trnID" column="trn_id" not-null="false"/>
<property name="dateTime" column="date_time" not-null="false"/>
<property name="suiteID" column="suitet_id" not-null="true"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
DTimer s = (DTimer)it.next();
//s.setTimerID(null);
myMachineSession.saveOrUpdate(s);
myMachineSession.flush();
myMachineTX.commit();

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MYSQL 3.2

Debug level Hibernate log excerpt:
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

As far as i know:
the "unsaved-value" tells hibernate when a instance is to be saved or updated:

id equals unsaved-value -> instance was never saved before -> assign id and insert
id not equals unsaved-value -> instance exists in DB -> update

perhaps Session.saveOrUpdateCopy() can do what you need ?

pascal
 
Stuart Houston
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update updates an existing record and save inserts a new record, saveOrUpdate checks then inserts or updates based on the value of the primary key. BUT what happens if you are copying from one table to another and the row in the table you are inserting to is empty BUT the ID key of the object you are inserting is not, Ie ID is not null.

How do i tell it to check the value of the key being held(ID) against the table its inserting into
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you persist an instance with an id, then hibernate thinks it exists and tries to update it.
doesn't the saveOrUpdateCopy() method help ? i never used it but the following seems to be what you want:

If the given instance is unsaved or does not exist in the database, save it and return it as a newly persistent instance.



Here is the full text

saveOrUpdateCopy

public Object saveOrUpdateCopy(Object object)
throws HibernateException

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved or does not exist in the database, save it and return it as a newly persistent instance. Otherwise, the given instance does not become associated with the session.

Parameters:
object - a transient instance with state to be copied
Returns:
an updated persistent instance
Throws:
HibernateException


cheers

pascal
 
Stuart Houston
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I did try it...or at least i wanted to, but when i insert the following into the code it throws a compiler error?

myMachineSession.saveOrUpdateCopy(dEpFilesInObj2);

Error 123,37) Cannot resolve symbol method saveOrUpdateCopy(DEpFilesIn)

any ideas? i cant think of why it should do this
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you update to latest version of hibernate ? the saveOrUpdateCopy has been added lately. (i have 2.1.4 here and method is in there), latest is 2.1.6

cheers


pascal
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Houston:
Hi

I did try it...or at least i wanted to, but when i insert the following into the code it throws a compiler error?

myMachineSession.saveOrUpdateCopy(dEpFilesInObj2);

Error 123,37) Cannot resolve symbol method saveOrUpdateCopy(DEpFilesIn)

any ideas? i cant think of why it should do this


I think, you MUST Download Lasted version of hibernate at

http://www.hibernate.org
 
Gavin King
author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two ways:

(1) add a <version> or <timestamp> property
(2) use Interceptor.isUnsaved() to distinguish, in your own code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic