How to define database generated column values as readonly fields in JPA and Hibernate?(如何将数据库生成的列值定义为 JPA 和 Hibernate 中的只读字段?)
问题描述
使用 MariaDB 10.2,可以为 Datetime 定义默认值,例如created 和 lastModified.
With MariaDB 10.2 it's possible to define default value for Datetime e.g. created and lastModified.
我应该如何将这些列作为只读字段访问?因为这个值应该只在数据库的控制下,不应该从代码中修改,但我想在代码中读取这个属性.
How I should access this columns as readonly field? Because this values should only be under control of the database and should not be modified from code, but I want read access to this property in code.
推荐答案
很简单.只需将 insertable
和 updatable
属性设置为 false
.
It's simple. Just set the insertable
and updatable
attributes to false
.
@Column(
name = "created_on",
insertable = false,
updatable = false
)
private Timestamp createdOn;
这篇关于如何将数据库生成的列值定义为 JPA 和 Hibernate 中的只读字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将数据库生成的列值定义为 JPA 和 Hibernate 中的只读字段?


基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01