Hibernate HQL查询不会更新Lucene索引 [英] Hibernate HQL query does not update the Lucene Index

查看:170
本文介绍了Hibernate HQL查询不会更新Lucene索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate 3.6.3 Final和Hibernate Search 3.4.1。
我写了一个HQL删除查询。这些对象将从数据库中删除,但在事务完成后,它们不会从Lucene索引中删除。
这里是查询:

  Session session = factory.getCurrentSession(); 
Query q = session.createQuery(Delete from Charges cg where cg.id in(:charges));
q.setParameterList(charges,chargeIds);
int result = q.executeUpdate();`

我错过了什么?我需要做什么来解决问题?



我创建了PostDeleteEvent,但FullTextEventListener似乎没有收到事件:

  SessionImpl sessImpl =(SessionImpl)factory.getCurrentSession(); 
SessionImplementor implementationor = sessImpl.getPersistenceContext()。getSession();
EntityPersister persister = implementor.getEntityPersister(Charges,cg);
EntityEntry entry = sessImpl.getPersistenceContext()。getEntry(cg);

Object [] deletedState = new Object []
{cg};
entry.setDeletedState(deletedState);
PostDeleteEvent pdEvent = new PostDeleteEvent(entry,entry.getId(),deletedState,
entry.getPersister(),(EventSource)sessImpl);`

谢谢。

解决方案

这是一个预期的限制,记录在Hibernate Search引用中。



HQL更新语句被解释为


  • 生成批量SQL以执行操作

  • 使任何相关缓存无效(如果使用任何二级缓存)

  • 查看是否需要刷新挂起的操作到数据库之前执行查询



但它不会从数据库加载内存中的所有潜在匹配项!



仍然Lucene需要内存中的元素,所以这确实是一个设计限制并且是预期的:您不应该对索引类型运行mass-update语句,而是在内存中对它们进行迭代,并在循环中对实体进行更改。



加载所有实体将会很慢,因为它需要实现内存中的所有数据,但无论如何,这需要喂给Lucene;一个好的二级缓存配置通常可以解决这个问题,或者只需启动MassIndexer就可以重新同步所有数据。


I am using Hibernate 3.6.3 Final and Hibernate Search 3.4.1. I wrote an HQL delete query. The objects are deleted from the database but they are not removed from the Lucene Index after the transaction completes. Here is the query:

Session session = factory.getCurrentSession();
Query q = session.createQuery("delete from Charges cg where cg.id in (:charges)");
q.setParameterList("charges", chargeIds);
int result = q.executeUpdate();`

What am I missing? What do I need to do to solve issue?

I created a PostDeleteEvent, hoever the FullTextEventListener doesn't appear to be receiving the event:

 SessionImpl sessImpl = (SessionImpl) factory.getCurrentSession();
 SessionImplementor implementor = sessImpl.getPersistenceContext().getSession();
 EntityPersister persister = implementor.getEntityPersister("Charges", cg);
 EntityEntry entry = sessImpl.getPersistenceContext().getEntry(cg);

 Object[] deletedState = new Object[]
 { cg};
 entry.setDeletedState(deletedState);
 PostDeleteEvent pdEvent = new PostDeleteEvent(entry, entry.getId(), deletedState,
                    entry.getPersister(), (EventSource) sessImpl);`

Thank you.

解决方案

This is an expected limitation, documented in the Hibernate Search reference.

HQL update statements are interpreted to

  • Generate the batch SQL to perform the operation
  • Invalidate any relevant cache (if using any second level cache)
  • See if pending operations need to be flushed to the database before executing the query

But it's not going to load all potential matches in memory from the database! That would kill performance.

Still Lucene requires the elements in memory, so indeed this is a design limitation and is expected: you should not run mass-update statements on indexed types, but rather iterate on them in memory and apply changes on the entities in a loop.

Loading all entities will be slow as it will need to materialize all data in memory, but that's required to feed Lucene anyway; a good second level cache configuration usually does the trick, or just start the MassIndexer to re-synch it all if changes are massive.

这篇关于Hibernate HQL查询不会更新Lucene索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆