我希望 JPA 实体中的一些 mycoulmns 在插入/更新期间从数据库中获取值,即从 Oracle sys_context,我相信 JPA 时间注释包括数据库在插入/更新期间生成的时间戳,有什么方法可以创建自定义注释与此有些相似或在插入/更新期间提供一些默认值,如果您遇到这种情况,有人可以提供一些指示。
最佳答案
I want some of mycoulmns in the JPA entity to take values from database during inserts/updates
将@Column
配置为不可插入和不可更新,并用@Generated
注释该字段(Hibernate 特定注释)。通过这样做,Hibernate 将在插入、更新后从数据库中获取值。来自引用文档:
2.4.3.5. Generated properties
Some properties are generated at insert or update time by your database. Hibernate can deal with such properties and triggers a subsequent select to read these properties.
@Entity public class Antenna { @Id public Integer id; @Generated(GenerationTime.ALWAYS) @Column(insertable = false, updatable = false) public String longitude; @Generated(GenerationTime.INSERT) @Column(insertable = false) public String latitude; }
Annotate your property as
@Generated
You have to make sure your insertability or updatability does not conflict with the generation strategy you have chosen. WhenGenerationTime.INSERT
is chosen, the property must not contains insertable columns, whenGenerationTime.ALWAYS
is chosen, the property must not contains insertable nor updatable columns.
@Version
properties cannot be@Generated(INSERT)
by design, it has to be eitherNEVER
orALWAYS
.
关于java - 创建自定义 JPA 时间注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3276599/