jpa2 强制 em 刷新设置为 insertable=false 的字段

标签 jpa jpa-2.0

我正在使用 jpa 2.0,我在数据库的表中有一个默认值的字段,我将该字段放在我的实体定义中作为 insertable = false 以便在插入时保留默认值,插入已正确完成,但是当重新查询实体中该字段为空的对象时,它已被正确插入。

这是我的代码:

@Entity
@Table(name="SOME_TABLE")
public class SomeTable implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private SomeTablePK id;

@Column(name="X1")
private String x1;

**@Column(name="X2", insertable=false)**
private Date x2;

... more fields....

... setters and getters...
}

有什么方法可以强制实体管理器刷新我用作 insertable = false 的字段的值?或者我能做些什么来解决它?

非常感谢。

附言。重要的是要在我的 persistence.xml 中提及这一点,并放置以下行以禁用缓存。

<properties>
<property name="javax.persistence.sharedCache.mode" value="NONE"/>
</properties>

最佳答案

您需要在刷新操作后手动调用刷新。

规范(3.2.4 与数据库同步)说:

The state of persistent entities is synchronized to the database at transaction commit. This synchroniza- tion involves writing to the database any updates to persistent entities and their relationships as speci- fied above.

An update to the state of an entity includes both the assignment of a new value to a persistent property or field of the entity as well as the modification of a mutable value of a persistent property or field[28].

注意以下:

Synchronization to the database does not involve a refresh of any managed entities unless the refresh operation is explicitly invoked on those entities or cascaded to them as a result of the specification of the cascade=REFRESH or cascade=ALL annotation element value.

关于jpa2 强制 em 刷新设置为 insertable=false 的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26637114/

相关文章:

java - 测试 Pageable 的意义何在?

java - 在 jpa 2 Criteria API 中选择特定列?

java - eclipselink 和 jpa : select id association with left join return "0" instead null

java - JPA与eclipse链接结果列表迭代到无穷大

java - 获取连接返回的行数超出应有的行数

java - 无法使用 POSTMAN 删除(映射)我的数据库的对象

java - gradle:找不到@Entity符号

mysql - 针对多个相似选择查询的 Hibernate 性能调优

java - 如果我使用 Java 中的 JPA 搜索 child 的 parent 之一,如何获得 child 的姓名作为结果?

java - JPA "cannot be cast to java.sql.Blob"