如何将数据库生成的列值定义为 JPA 和 Hibernate 中的只读字段?

2022-01-15 00:00:00 mariadb java Hibernate jpa readonly

使用 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;

相关文章