aspose file tools*
The moose likes Object Relational Mapping and the fly likes Am I persisting a detached Entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Am I persisting a detached Entity?" Watch "Am I persisting a detached Entity?" New topic
Author

Am I persisting a detached Entity?

Cainiao Zou
Ranch Hand

Joined: Mar 03, 2009
Posts: 36
I have a entity "User", and i just create it and try to persist it, but I got a error message, something like i was persisting a detached Entity.
I checked the code of "User", i found there was no auto generated id property like other entities, the identity is the "name" property, which must be initialed by creating. Then I added the "dummy" id property just like normal, all work fine!

So I just wonder, why JPA/Hibernate said it was a detached Entity at all? I guess, it just checks the identity property of the entity, if it's not NULL (dummy ids are almost always null by creating and will be auto generated), and there is no copy in PersistenceContext with the same identity, then it will be considered as detached?

Am I right? But what about the situation, when i don't want to use the auto generated identity? How can I persist them?

thanks
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

...when i don't want to use the auto generated identity? How can I persist them?

You should have set the application-assigned key value before calling the persist() method since it is not auto-generated in this case.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Am I persisting a detached Entity?