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

后端 未结 5 1555
清歌不尽
清歌不尽 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:37

    Try

    @Column(updatable = false)
    

    And make your setter private. (Leave your getter public if you want)

    I think this is the best practice.

    P.S.: JPA uses field access if you annotate your fields and uses getter/setter access if you annotate your getter method.

提交回复
热议问题