如何使实体只读? [英] How to make an Entity read-only?
问题描述
使用 JPA 使实体只读的正确方法是什么?我希望我的数据库表永远不会以编程方式被修改.
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.
我想我明白我应该用 LockModeType.READ
锁定我的对象.从数据库中检索后是否可以使用注释使我的实体直接锁定?或者我是否必须为那个特定实体搞砸并覆盖我的通用 DAO?
I think I understand that I should lock my objects with LockModeType.READ
. Is it possible to use an annotation to make my entities directly locked after retrieval from the database ? Or do I have to mess around and override my generic DAO for that specific entity ?
推荐答案
一个解决方案是使用基于字段的注释,将您的字段声明为 protected
并仅建议公共 getter.这样做,您的对象将无法更改.
A solution is to use field based annotation, to declare your fields as protected
and to propose only public getter. Doing so, your objects can not be altered.
(这个解决方案不是特定于实体的,它只是一种构建不可变对象的方法)
(This solution is not entity specific, it is just a way to build immutable objects)
这篇关于如何使实体只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!