【问题标题】:Getting a TransientObjectException when creating my initial index创建初始索引时出现 TransientObjectException
【发布时间】:2012-04-05 20:50:46
【问题描述】:

我正在使用以下代码来初始化我的 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 计算返回值的方法。 HashMap 通过 Hibernate 检索如下:

@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

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    您的问题与Foo, 中的@Transient 注释无关,而是与整个Foo 有关。索引过程中的一些代码尝试调用Session.lock(foo),,但此时foo 仍然只是一个普通对象,在使用Session.saveSession.persist 分配给会话之前。正如here 所解释的那样,将cascade=PERSIST 参数添加到父对象中foo 字段的注释中可能会有所帮助。

    【讨论】:

    • 我说得太早了。似乎问题仍然存在。我有一个 Hibernate Search 团队的耳朵,所以我希望他们能提供帮助。示例代码可以在https://github.com/jsvazic/hibernate-search-example 找到
    • 只是一个快速更新,但我已将示例代码调整为非常简单。似乎问题出在 Division 类中,因为 DivisionText 工作得很好,即当我在 Division 上添加 @Indexed 注释时,它失败了。
    • 在您的示例中,com.example.Foo 在哪里?现在您有了该代码示例,请更新您的问题以反映它。
    • 好点,抱歉。该问题已更新以反映示例代码和更简单的情况。
    • 那么让我们回顾一下你所做的事情......你为什么在DivisionText.division 上没有cascade=PERSIST(或ALL)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2020-08-06
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多