我的目的是使用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()
方法从第一级缓存中清除已删除的实体?有没有官方文件解释这个问题?
谢谢。
答案 0 :(得分:1)
这是我的解决方案,但它真的不整洁。
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);
}
}