我正在使用 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/