File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Merging problem - id is generated instead of cusotm id. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Merging problem - id is generated instead of cusotm id." Watch "Merging problem - id is generated instead of cusotm id." New topic
Author

Merging problem - id is generated instead of cusotm id.

Dmitriy Kastikovich
Greenhorn

Joined: Jun 29, 2008
Posts: 23
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Jun 29, 2008
Posts: 23
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Merging problem - id is generated instead of cusotm id.
 
Similar Threads
Testing @ManyToOne Relationships
why they use hashCode() method in equals() method
sorting table columns
Application-managed persistence context in EJB
Query after JPA collective update not getting updates