创建初始索引时获取TransientObjectException

时间:2012-04-05 20:50:46

标签: hibernate full-text-search lucene hibernate-search

我正在使用以下代码初始化我的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的返回值的方法有两个HashMap注释。通过Hibernate检索HashMap,如下所示:

@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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

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