如何使实体成为只读? [英] How to make an Entity read-only?

查看:86
本文介绍了如何使实体成为只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆