-->
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.  [ 6 posts ] 
Author Message
 Post subject: Error while updating\deleting entity with null @Version
PostPosted: Fri Jun 23, 2017 9:20 am 
Newbie

Joined: Fri Jun 23, 2017 9:08 am
Posts: 3
Hi,

My wish is to have ability to perform updates and deletes on versioned entities (http://docs.oracle.com/javaee/6/api/javax/persistence/Version.html) without passing actual latest version manually - in this scenario updates\deletes should succeed. If i pass null in version column - hibernate treats entity as transient and perform insert instead of update. Setting actual version value before database operations is not a preferrable solution, since every service would be responsible for locking logic, etc. So i wonder if somebody knows how to workaround this issue.


Top
 Profile  
 
 Post subject: Re: Error while updating\deleting entity with null @Version
PostPosted: Fri Jun 23, 2017 10:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The default optimistic locking mechanism does not allow you to interact with the version attribute. That's just for Hibernate internal use only.

If you want to bump the version up manually, you have to use OPTIMISTIC_FORCE_INCREMENT.

Anyway, you should never set it to null manually. The only time when the version is allowed to be null is the first time you create an entity.

Therefore, you either have optimistic locking or not, there's no middle ground.


Top
 Profile  
 
 Post subject: Re: Error while updating\deleting entity with null @Version
PostPosted: Fri Jun 23, 2017 11:47 am 
Newbie

Joined: Fri Jun 23, 2017 9:08 am
Posts: 3
Hi Vlad, thanks for your reply.

Your comment about the irregularity of interaction with the version attribute is 100% correct. However, we found a way to make it work.

In the use case for the described problem we use the version field in the way of:
1) get DTO from user
2) assemble entity from DTO, for the simplicity assume that we do:
Code:
Entity entity = new Entity();
entity.setId(...);
entity.setVersion(...); // <-- here we manually set version to a detached entity and it works as it should, although it's not mentioned as a recommended solution
em.saveAndFlush(entity);

In the described scenario, opt locks are working well, and we are able to pass\recieve version from a user. But the desired option is ability not to provide a version at all in some cases and still perform updates\deletes.
Maybe there is an elegant way to automatically set the latest version.


Last edited by dmitrymikhailov on Mon Jun 26, 2017 4:55 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Error while updating\deleting entity with null @Version
PostPosted: Fri Jun 23, 2017 12:14 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I think you should use merge instead of saveAndFlush.

Merge allows you to fetch the latest entity snapshot, do the merge while still propagating the version, which is actually correct since otherwise you'd lose updates.

If you don't want to set the version by yourself, you need to save the entity in the Session context in between two HTTP requests or use the EXTENDED Persistence Context.


Top
 Profile  
 
 Post subject: Re: Error while updating\deleting entity with null @Version
PostPosted: Fri Jun 23, 2017 12:45 pm 
Newbie

Joined: Fri Jun 23, 2017 9:08 am
Posts: 3
Hmm.. I guess alongside with merge this can be handled with the manual locking mechanism, as described in this article.

I wonder if there is a similar way to control locking in runtime without manually removing @Version annotation (we still need the column to pass the version number to a user).


Top
 Profile  
 
 Post subject: Re: Error while updating\deleting entity with null @Version
PostPosted: Fri Jun 23, 2017 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
Hmm.. I guess alongside with merge this can be handled with the manual locking mechanism (as described in https://vladmihalcea.com/hibernate-lock ... mode-work/)


Nope, you don't need that. That's for checking a different entity version, not one that you changed.

You just need to pass the detached entity that you read in a previous request, and then you don't have to worry about what version it has.


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