3

我正在使用以下代码来初始化我的 Hibernate Search 索引:

EntityManager em = ...
FullTextEntityManager fullTextEM = Search.getFullTextEntityManager(em);
fullTextEM.createIndexer().startAndWait();

现在,当我执行此代码时,出现以下异常:

ERROR: HSEARCH000058: HSEARCH000116: Unexpected error during MassIndexer operation
org.hibernate.TransientObjectException: cannot lock an unsaved transient instance: com.example.hs.model.Division
at org.hibernate.event.internal.DefaultLockEventListener.onLock(DefaultLockEventListener.java:75)
at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:724)
at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:717)
at org.hibernate.internal.SessionImpl.access$1700(SessionImpl.java:170)
at org.hibernate.internal.SessionImpl$LockRequestImpl.lock(SessionImpl.java:2276)
at org.hibernate.search.batchindexing.impl.EntityConsumerLuceneWorkProducer.indexAllQueue(EntityConsumerLuceneWorkProducer.java:130)
at org.hibernate.search.batchindexing.impl.EntityConsumerLuceneWorkProducer.run(EntityConsumerLuceneWorkProducer.java:102)
at org.hibernate.search.batchindexing.impl.OptionallyWrapInJTATransaction.run(OptionallyWrapInJTATransaction.java:112)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

在这种情况下,com.example.hs.model.Division该类确实@Transient为从 a 计算返回值的方法提供了两个注释HashMap。通过HashMapHibernate 检索如下:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "division_id", referencedColumnName = "id")
@MapKey(name = "language")
@Field(name="name")
@FieldBridge(impl = com.example.hs.search.LanguageDivisionTextBridge.class)
protected Map<Language, DivisionText> getDivisionTextMap() {
    return divisionTextMap;
}

如您所见,我正在使用自定义FieldBridge来帮助映射。该类DivisionText也标有@Indexed注释并已成功编入索引。

完整的源代码可以在以下位置找到:

https://github.com/jsvazic/hibernate-search-example

任何帮助将不胜感激。

4

1 回答 1

3

@Transient您的问题与内部的注释无关,Foo,而是与Foo整体有关。索引过程中的某些代码正在尝试调用Session.lock(foo),,但此时foo它仍然只是一个普通对象,在使用Session.saveor分配给会话之前Session.persist。也许它会有助于在父对象cascade=PERSIST的字段上的注释中添加一个参数,如此所述。foo

于 2012-04-06T07:41:35.153 回答