Is it possible to have immutable fields in Hibernate/JPA?

后端 未结 5 1618
清歌不尽
清歌不尽 2021-02-07 03:00

In our application, we need to have fields that are assignable only once.

At first we thought of encapsulating the fields and making the setters private. However, some q

5条回答
  •  再見小時候
    2021-02-07 03:41

    You can mark an entity with @Entity(mutable=false) or @Immutable annotations for the framework to make use of this fact for performance gain in caching and such. (Hibernate)

    Then you can use an immutable wrapper class like this:

    public class ImmutableStuff {
        private final FooField barValue;
    
        public ImmutableStuff(Stuff stuff) {
            barValue = stuff.barValue;
        }
    
        public FooField getBarValue(){
            return barValue;
        }
    }
    

提交回复
热议问题