简体   繁体   English

如何在一次打开session中删除具有一级缓存和JPA的实体?

[英]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中的实体以提交更改。

  • The EntitySession EntitySession
transaction = entityManager.getTransaction();
  • The remove method删除方法
    public BehindCacheBuilder<R, V> remove(Object object) {
        entityManager.remove(object);
        return this;
    }
  • And committing the transaction并提交交易
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.

相关问题 如果我们有多个会话对象,一级缓存如何工作? - how first level cache works, if we have multiple session objects? 一级缓存不适用于 JPA 和 Hibernate - First level cache not working with JPA and Hibernate JPA和一级缓存,重点是什么? - JPA and first level cache, whats the point? 为每个 session 的一级缓存分配 memory - Allocating memory for first level cache per a session 如何创建JPA二级缓存以与实体的辅助键一起使用? - How do I create a JPA second-level cache for use with an entity's secondary key? 如何将Hibernate二级缓存寿命设置为仅在Wildfly上部署的一个实体(Infinispan) - how to set Hibernate second level cache lifespan to just one entity deploying on Wildfly (Infinispan) 如何在JPA中使用Hibernate的二级缓存? - How do I use Hibernate's second level cache with JPA? 如何在XML中为JPA中的二级缓存定义可缓存 - How to define cacheable in XML for second level cache in jpa 在 Spring Data JPA 中,派生的 find 方法在一个事务中多次调用时不使用一级缓存,但默认的 findById 会 - In Spring Data JPA, a derived find method doesn't use first-level cache while called multiple times in one transaction, but the default findById does Hibernate一级缓存是否只能单向同步? - Is the Hibernate first-level cache only synchronized one-way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM