gpt4 book ai didi

java - Hibernate:使用executeUpdate()批量删除不会清除一级缓存

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:38 24 4
gpt4 key购买 nike

我的目的是使用 executeUpdate() 执行单个 DELETE,而不是循环遍历 session.delete(),这非常繁琐。

我面临的问题是通过执行executeUpdate(),它不会从一级缓存中逐出现有的已删除实体。

这是一个非常简单的例子:-

final Session session = sessionFactory.getCurrentSession();

System.out.println("---------------------------------");
System.out.println("What's currently in 1st level cache");
System.out.println("---------------------------------");

for (Object o : session.getStatistics().getEntityKeys()) {
EntityKey entityKey = (EntityKey) o;
System.out.println(entityKey.getEntityName() + " -> " + entityKey.getIdentifier());
}

System.out.println("---------------------------------");
System.out.println("Ensure there are 3 project users");
System.out.println("---------------------------------");

checkArgument(session.createQuery("from ProjectUser").list().size() == 3);

System.out.println("---------------------------------");
System.out.println("Deleting all 3 project users");
System.out.println("---------------------------------");

checkArgument(session.createQuery("delete from ProjectUser").executeUpdate() == 3);

session.flush();

System.out.println("---------------------------------");
System.out.println("Latest state in 1st level cache");
System.out.println("---------------------------------");

for (Object o : session.getStatistics().getEntityKeys()) {
EntityKey entityKey = (EntityKey) o;
System.out.println(entityKey.getEntityName() + " -> " + entityKey.getIdentifier());
}

这是我看到的打印输出:-

---------------------------------
What's currently in 1st level cache
---------------------------------
testHibernate.domain.ProjectUser -> 2
testHibernate.domain.ProjectUser -> 1
testHibernate.domain.User -> 1
testHibernate.domain.ProjectUser -> 3
testHibernate.domain.User -> 3
testHibernate.domain.Project -> 1
testHibernate.domain.User -> 2
---------------------------------
Ensure there are 3 project users
---------------------------------
Hibernate:
select
projectuse0_.projectUserId as projectU1_1_,
projectuse0_.datetime as datetime2_1_,
projectuse0_.projectId as projectI3_1_,
projectuse0_.userId as userId4_1_
from
projectUser projectuse0_
[TRACE] [BasicExtractor] [extract:74] - Found [1] as column [projectU1_1_]
[TRACE] [BasicExtractor] [extract:74] - Found [2] as column [projectU1_1_]
[TRACE] [BasicExtractor] [extract:74] - Found [3] as column [projectU1_1_]
---------------------------------
Deleting all 3 project users
---------------------------------
Hibernate:
delete
from
projectUser
---------------------------------
Latest state in 1st level cache
---------------------------------
testHibernate.domain.ProjectUser -> 2
testHibernate.domain.ProjectUser -> 1
testHibernate.domain.User -> 1
testHibernate.domain.ProjectUser -> 3
testHibernate.domain.User -> 3
testHibernate.domain.Project -> 1
testHibernate.domain.User -> 2

如您所见,testHibernate.domain.ProjectUser 实体仍然徘徊在一级缓存中,即使它们已被删除。

我的问题是如何使用 executeUpdate() 方法从一级缓存中清除已删除的实体?有没有官方文档解释这个问题?

谢谢。

最佳答案

这是我的解决方案,但它确实很不整洁。

BaseEntity 只是我们所有实体的基类。

@PersistenceContext
transient EntityManager entityManager;

@SuppressWarnings("unchecked")
public void evict(Class<? extends BaseEntity> c)
{
HibernateEntityManager hem = entityManager.unwrap(HibernateEntityManager.class);

Session session = hem.getSession();

Query query = session.createQuery("select o from " + c.getSimpleName() + " o where isObsolete = false AND isDeleted = false");

List<? extends BaseEntity> entities = (List<? extends BaseEntity>) query.list();

for (BaseEntity e : entities)
{
session.evict(e);
}
}

关于java - Hibernate:使用executeUpdate()批量删除不会清除一级缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28368318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com