如何将数据库生成的列值定义为 JPA 和 Hibernate 中的只读字段? [英] How to define database generated column values as readonly fields in JPA and Hibernate?

查看:25
本文介绍了如何将数据库生成的列值定义为 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.

推荐答案

很简单.只需将 insertableupdatable 属性设置为 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 中的只读字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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