[hibernate-issues] [Hibernate-JIRA] Commented: (ANN-609) Need a way to specify "unsaved-value" strategy using annotations
Suji Suresh (JIRA)
noreply at atlassian.com
Sat May 5 16:24:04 EDT 2007
[ http://opensource.atlassian.com/projects/hibernate/browse/ANN-609?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_26861 ]
Suji Suresh commented on ANN-609:
---------------------------------
The unsaved-value strategy is absolutely necessary for objects that have the primary key value assigned otherwise there is no way for Hibernate to tell the difference between a new object from an existing object and therefore it throws a StaleStateException instead of DataIntegrityViolationException. This is because Hibernate uses the default unsaved-value strategy which a non-null value on the primary key field. In my case where I assign the value for the primary key before passing it on to Hibernate, the primary key field is never null so Hibernate always assumes that it is an existing object and therefore tries to merge with an existing object in the database and if the object is already present in the database it throws a StaleStateException because the value of "row version" field is lesser than the value present in the database.
Consider Object A that has a primary key field "id". Object A also has a "rowVersion" field. I give Hibernate a new Object A "a" with id value 123 and rowVersion 0. Hibernate assigns a rowVersion 1 and adds this object no problem. Now I give Hibernate another Object A "a1" with id value 123 and rowVersion 0. Hibernate attempts to add this object but instead finds another Object A with a rowVersion greater than a1's rowVersion and fails with StaleStateException. Now this same usecase will work just fine if I attach an "unsaved-value" of -1 to the rowVersion. This will allow Hibernate to distinguish between a new object (the object with rowVersion -1) from an existing object (an object with rowVersion greater than 0)
I hope I was able to clarify my problem
> Need a way to specify "unsaved-value" strategy using annotations
> ----------------------------------------------------------------
>
> Key: ANN-609
> URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-609
> Project: Hibernate Annotations
> Issue Type: New Feature
> Environment: Hibernate 3.2.2, Hibernate-annotations 3.2.1
> Reporter: Suji Suresh
> Priority: Minor
>
> Hibernate has two ways of specifying a value for primary key:
> 1. Assign a value before handing it over to Hibernate
> 2. Have Hibernate generate the value before persisting
> In my project I have objects of both of the above specified types. In other words I assign value for the primary key for some the objects (lets call these objects "assigned") and for others I have Hibernate generate the value before persisting (lets call these objects "generated"). Since Hibernate annotations does not support "unsaved-value", I do not have a way of specifying "unsaved-value" strategy. While my "generated" objects work perfectly with Hibernate's default "unsaved-value" strategy, I see wrong (but expected) behaviour when I try to persist an "assigned" object that is already present in the database in that, I get StaleStateException instead of DataIntegrityViolationException
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list