How to make an Entity read-only?

后端 未结 9 1262
情歌与酒
情歌与酒 2020-12-03 00:43

What is the proper way to make an Entity read-only with JPA ? I wish my database table to never be modified at all programmatically.

I think I understand that I sho

9条回答
  •  -上瘾入骨i
    2020-12-03 00:47

    If you are using spring-data or are otherwise using the Repository pattern, don't include any save / update / create / insert / etc methods in the Repository for that particular entity. This can be generalized by having a base class / interface for readonly entities, and an updatable one that extends the readonly one for updatable entities. As other posters have pointed out, the setters may also be made non-public to avoid developers accidentally setting values that they are then unable to save.

提交回复
热议问题