简体   繁体   English

Hibernate HQL查询不会更新Lucene索引

[英]Hibernate HQL query does not update the Lucene Index

I am using Hibernate 3.6.3 Final and Hibernate Search 3.4.1. 我正在使用Hibernate 3.6.3 Final和Hibernate Search 3.4.1。 I wrote an HQL delete query. 我写了一个HQL删除查询。 The objects are deleted from the database but they are not removed from the Lucene Index after the transaction completes. 事务完成后,将从数据库中删除对象,但不会将它们从Lucene Index中删除。 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: 我创建了一个PostDeleteEvent,但是FullTextEventListener似乎没有收到事件:

 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. 这是预期的限制,已在Hibernate Search参考中记录。

HQL update statements are interpreted to HQL更新语句被解释为

  • Generate the batch SQL to perform the operation 生成批处理SQL以执行操作
  • 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. Lucene仍然需要内存中的元素,因此确实是设计上的限制,这是可以预期的:您不应在索引类型上运行mass-update语句,而应在内存中对其进行迭代并在循环中对实体应用更改。

Loading all entities will be slow as it will need to materialize all data in memory, but that's required to feed Lucene anyway; 加载所有实体会很慢,因为它将需要具体化内存中的所有数据,但是无论如何,这都需要喂给Lucene。 a good second level cache configuration usually does the trick, or just start the MassIndexer to re-synch it all if changes are massive. 一个好的二级缓存配置通常可以解决问题,或者,如果更改很大,则只需启动MassIndexer即可重新同步所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM