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 中的只读字段?


- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01