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: Save row with new ID
PostPosted: Tue Jul 18, 2006 2:23 pm 
Newbie

Joined: Tue Mar 08, 2005 2:08 pm
Posts: 5
Hibernate version: 2.1.8

I need to load a row from the database and save a copy of it under a new id. The class is configured with a sequence, and that part works fine when I have a new object.

Widget data = (Widget) session.get(Widget.class, id);
data.setId(null);
session.saveOrUpdate(data);
session.flush();

When I do this I'm getting the following exception:
net.sf.hibernate.HibernateException: identifier of an instance of com.x.y.z.Widget altered from 100174345 to null

What steps am I missing? I really don't want to have to write code to copy the properties to a new instance as there are many properties and I have to do this with several tables.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 4:53 pm 
Newbie

Joined: Tue Jul 18, 2006 2:50 pm
Posts: 3
I don't think you can set your ID to null. Is ID a primary key that auto increments at your database? If it does, then you want to configure your Widget.hbm.xml file to say auto increment the next ID for me.

For example, Widget.hbm.xml could look something like this
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping >
 
  <class name="x.y.z.Widget" table="Widget">
    <id name="id" column="id" type="java.lang.Integer">
      <generator class="increment"/>
    </id>
...
  </class>
 
</hibernate-mapping>


So, the <generator> tag will say to increment the ID column.

And I believe if you want to assign your own ID value, then you could probably try <generator class="assigned"/> instead.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 2:04 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
You may want to look at creating a new instance of the Widget then use a DTO or a mapping routine to populate what you want from the old Widget to the new Widget, then saveOrUpdate() on the new instance.

You are trying to saveOrUpdate() a persistent object retrieved from the database. You probably already know that if there is not pk then save(insert) happens, if the pk is populated the update happens. Trying to make a pk null is probably not good. Null and blank (not populated) are different.

Your code is trying to update an existing record to have a null pk - not insert a new record.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:55 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Close the current Hibernate session (you could also try session.evict or session.clear) and save in a new Hibernate session after setting the id to null. Think it should work. You could get into trouble only if your class has any collection. It would then require a few more steps.


Top
 Profile  
 
 Post subject: Re: Save row with new ID
PostPosted: Sat Oct 23, 2010 7:50 am 
Newbie

Joined: Sat Oct 23, 2010 7:01 am
Posts: 1
i was also looking for something of that sort
say i have
table A with one record of id = 1;
table A has a collection which refers to table B
table B has a collection which refers to table C.

so now i want all the data from A and store it as a new row with id=2
but at the same time i want the collection data to be persisted as new rows in table B and table C.

what extra steps do i have to go though to ensure that all the associations are also being saved in the DB reference the row with the new ID


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.