-->
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.  [ 7 posts ] 
Author Message
 Post subject: Session.replicate() into IDENTITY table ?
PostPosted: Tue Nov 01, 2005 5:11 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
Hibernate 3.0.5

I am trying to populate an empty table whose primary key is an IDENTITY column, however I need to leave holes in the data and already know the exact object id value I wish to set. My understanding of Session.replicate() is thats the method for the job, "Persist the state of the given detached instance, reusing the current identifier value".

Before I open my Hibnerate session I issue a "TRUNCATE mytable" on the SQL server, this deletes all rows and resets the IDENTITY column value to 1.

My Hibernate mapping has <generator class="native" /> which is the correct value I want for my application after this overriding data population process has completed.

My code looks something like:

Code:
MyObject myobj = new MyObject();
myobj.setId(999999);
myobj.setOtherData("foobar");
hsession.replicate(myobj, ReplicationMode.EXCEPTION);


However the INSERT statements generated omit the primary key column, so my SQL server obliges and assumes I want IDENTITY generation. So the object id for this instance is 1 not 999999.

Code:
Hibernate: select id from mytable where id =?
Hibernate: insert into mytable (other_data) values (?)



If I were to change the <generator class="assigned" /> I'm sure it would work, but so would Session.save(). So whats does Session.replication() do if it doesn't cover this scenario ?

Suggestions ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 2:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The problem with replicate and identity columns is that some (most?) databases don't let you insert an explicit value into an identity column.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 4:11 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
Thanks for the reply, I'm using MySQL which I believe does allow this.

Does this mean my understanding of Session.replicate() is correct and makes sense in this case ?

Would I be wrong in suggesting that Hibernate should throw an exception if it was not able to honour keeping the current ID value. From a programmers point of view this was the intended behaviour, otherwise he would have called Session.save().

If the programmer does not actually care then maybe a Session.replicateOrSave() could be introduced to let him choose his case.

Once its throwing exceptions in cases where I can't honer the assigned id (or does not know if it can), then have a configurable override per class in the XML config that allows the programmer to know best and enable running of the INSERT with primary key assigned from Session.replicate(). This would be enough to get me out of the situation.

Then maybe in the future it would be possible to use database dialect and runtime driver/server versioning to auto-configure those cases that were able to honour assigned ID value even in IDENTITY tables.


Is there anything alternative with Hibernate I could use, or is JDBC my way out here ?


Code:
mysql> create table foobar ( id BIGINT AUTO_INCREMENT, value INT, PRIMARY KEY(id));
Query OK, 0 rows affected (0.14 sec)

mysql> insert into foobar(id, value) VALUES('999999', '42');
Query OK, 1 row affected (0.14 sec)

mysql> insert into foobar(value) VALUES('43');
Query OK, 1 row affected (0.00 sec)

mysql> select * from foobar;
+---------+-------+
| id      | value |
+---------+-------+
|  999999 |    42 |
| 1000000 |    43 |
+---------+-------+
2 rows in set (0.00 sec)

mysql>


The above is exactly what I want to do.


I did try to work around the problem by defining two mappings to two different POJO classes, but one with ASSIGNED key generation. And simply make the assigned POJO a super-class of the native generated one (class MyClassAssigned extends MyClassNative) This works during the population process, but with both maps loaded into the the Hibernate Session my HQL query of the sub-class:

FROM MyClassNative AS rec WHERE rec.columnOnUniqueKey = ?

Does two SELECTs one for each type and my application starts to see everything in double vision. I'm sure mapping the same table in two different ways must do bad things, so I've stopped that idea.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 10:23 am 
Newbie

Joined: Mon Jun 04, 2007 9:47 am
Posts: 4
Hello,
I have submitted an implementation proposal http://opensource.atlassian.com/project ... e/HHH-2716


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 6:59 am 
Newbie

Joined: Wed Apr 12, 2006 7:02 am
Posts: 7
Location: Southampton, UK
Hi,

we are facing the same problem with replicate using mysql 5. has anybody got a workaround or fix for this yet?

thanks in advance,

John


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 11:32 am 
Newbie

Joined: Fri Feb 08, 2008 2:41 pm
Posts: 18
Hi,

I am having the same problem and need a fix as soon as possible.
Are they going to fix this issue or not?
It is on the JIRA since 0/Jul/07 and still unassigned.
Did you find a workaround in the mean time?


Top
 Profile  
 
 Post subject: Re: Session.replicate() into IDENTITY table ?
PostPosted: Wed Nov 10, 2010 5:06 pm 
Newbie

Joined: Wed Nov 10, 2010 4:24 pm
Posts: 6
Anybody ever find a work around on this? I know it's like 2010 and all, but I'm having the same problem with the latest release. I'm replicating objects into another datastore and the existing id keeps getting regenerated instead reused.


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