Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Merging problem - id is generated instead of cusotm id.

 
Dmitriy Kastikovich
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I want to update row with some id, but the result is createing a new row.
So I have entity:



Then I do so(see that the ids is the same):



Result: two rows with not my id:
ID | VALUE
---------------------------
1 | value=123456789
2 | value=987654321


So, is it possible in Hibernate to update row in such way ?
Thanks.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just kicking at the can here.

Hibernate sees two detached objects here. Since it didn't load either of them, it doesn't know if they have real representations in the database. When it goes to merge them, it sees that both do not have database representations, and it then creates the db representation, and it creates new ids for both, since each new record is told to use an id from the sequence.

Can you load the one record first, and then merge the new one and see what happens?



Just an idea. I'm speaking purely out of ignorance on this one.

-Cameron
 
Dmitriy Kastikovich
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Wallace McKenzie wrote:

Can you load the one record first, and then merge the new one and see what happens?



Just an idea. I'm speaking purely out of ignorance on this one.


No, I cant load first. I need a hand-set ID on already created entities with id-generators.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can load first!

I'm suggesting this not as a final solution, but to see what happens when you initially load an object, and then do a merge. That way you don't have transient objects, but instead, are merging a transient object with a persistent objects.

Doing this as a test case might help you solve your problem, and discover what is going wrong. Then you can adopt your final solution as fit.

It's just an idea. I'd be curious as to why you can't do a test that loads one entity first?

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic