Is there a configurable alternative to using @Version?

后端 未结 1 682
醉话见心
醉话见心 2021-01-24 12:44

I\'m placing an annotated field with @Version on it in all my JPA domain classes, however this just seems like additional boiler plate. Is there a way to get aroun

相关标签:
1条回答
  • 2021-01-24 13:24

    As far as the JPA specification tells us you can't change the @Version annotation via "configuration". You either use @Version in your program code or you don't.

    Referring to the official JPA specification (final version, JPA 2.1) in Section 3.4.2 (page 90) we find:

    An entity is automatically enabled for optimistic locking if it has a property or field mapped with a Version mapping.

    [...]

    If only some entities contain version attributes, the persistence provider runtime is required to check those entities for which version attributes have been specified. The consistency of the object graph is not guaranteed, but the absence of version attributes on some of the entities will not stop operations from completing.

    However, you can use the concept of inheritance to provide the @Versiononly in one spot via an abstract base class. This class you be written as follows:

    @MappedSuperclass
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    public abstract class AbstractBaseEntity { 
    
        public static final long INVALID_OBJECT_ID = -42;
    
        @Version
        private int version;    
    
        @Id
        @SequenceGenerator(name = "sequence-object", sequenceName = "ID_MASTER_SEQ")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence-object")
        @Column(name = "id")
        protected Long objectID = INVALID_OBJECT_ID;
    
        public final int getVersion() {
            return version;
        }
    
        @Override
        public long getObjectID() {
            return objectID;
        }
    
        // ... maybe other methods or fields ...
    }
    

    Thus, all your @Entity annotated sub-classes that inherit from AbstractPersistentEntity are provided with both properties: (i) objectIDand (ii) version at once. For instance, class SomeClass can be written as:

    @Entity
    public class SomeClass extends AbstractBaseEntity /*implements SomeInterface*/ {
        // ... specific methods or fields ...
    } 
    

    For details on the use of @MappedSuperclass see also this answer.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题