简体   繁体   中英

jpa way to see if entity or one of its relations has changed version

I'm looking for a way to find out if a detached entity has changed in the database. Furthermore I want to know if any of its relations with cascade = CascadeType.MERGE has changed. The entity and some of its relations do have a version field with @Version annotation. Basically the test should report if a merge is going to throw an OptimisticLockException without trying to do the merge itself. Of course I could go through all relations and compare the version fields, but I'd like to have a more change resistant solution. So if someone adds a relation in the future the test should still work, without adopting.

Is there a standard way of doing this?

JPA 2.0 have Optimistic Locking and Pessimistic Locking .

I think, you already know Optimistic Locking for main entity like as below.

在此处输入图片说明

I'm looking for a way to find out if a detached entity has changed in the database?

The below example is Optimistic Locking handling for detached entity. It is using LockModeType.OPTIMISTIC_FORCE_INCREMENT .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM