[英]How to remove entity with first level cache and JPA in one open session?
In my persistence layer i have remove
method that will remove the entities in removed state and EntitySession
to commit the changes.在我的持久层中,我有
remove
方法,该方法将删除已删除的 state 和EntitySession
中的实体以提交更改。
EntitySession
EntitySession
transaction = entityManager.getTransaction();
public BehindCacheBuilder<R, V> remove(Object object) {
entityManager.remove(object);
return this;
}
transaction.commit();
But when i execute the method nothing will remove from datasource.但是当我执行该方法时,不会从数据源中删除任何内容。
Like any write-behind cache the Persistence context requires flushing (in your case committing, which is different than flushing) in order to synchronize the in-memory persistence state with underlying datasource.与任何后写缓存一样,持久性上下文需要刷新(在您的情况下提交,这与刷新不同)以便将内存持久性 state 与底层数据源同步。
Thus firstly make sure your entities that you want to remove be present in Managed state from first place then try to remove
to change the state for delating the entity after flushing (committing).因此,首先确保您要删除的实体首先出现在托管 state 中,然后尝试
remove
以更改 state 以在刷新(提交)后删除实体。
public BehindCacheBuilder<R, V> remove(Class<?> type, Object object) {
entityManager.remove(entityManager.find(type, object));
return this;
}
Which in here we bring the entity that we want to remove to Managed state.在这里,我们将要删除的实体带到托管state。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.