简体   繁体   English

使用JTA事务进行Hibernate搜索和JPA

[英]Hibernate search and JPA with JTA transaction

I want to use the Hibernate Search full text search capabilities. 我想使用Hibernate Search全文搜索功能。 I have a simple Java EE application. 我有一个简单的Java EE应用程序。 I annotated the entity classes and here is my persistence.xml: 我注释了实体类,这是我的persistence.xml:

<persistence-unit name="library">
    <jta-data-source>jdbc/webrarydb</jta-data-source>
    <class>net.hcpeter.webrary.entities.Author</class>
    <class>net.hcpeter.webrary.entities.Book</class>
    <class>net.hcpeter.webrary.entities.Request</class>
    <class>net.hcpeter.webrary.entities.User</class>

    <properties>
        <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider"/>
        <property name="hibernate.search.indexing_strategy" value="manual"/>
        <property name="hibernate.search.default.indexBase" value="/Users/hcpeter/Documents/workspace/indexes"/>
        <property name="hibernate.current_session_context_class" value="org.hibernate.context.JTASessionContext"/>
    </properties>
</persistence-unit>

And I try to search this way: 我尝试通过这种方式进行搜索:

EntityManager em = authorFacade.getEntityManager();
        FullTextEntityManager ftem = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);

        ftem.getTransaction().begin();
        QueryBuilder qb = ftem.getSearchFactory().buildQueryBuilder().forEntity(Author.class).get();
        org.apache.lucene.search.Query query = qb.keyword().onFields("firsName", "lastName").matching("Author#1").createQuery();

        javax.persistence.Query persistenceQuery = ftem.createFullTextQuery(query, Author.class);
        List<Author> result = persistenceQuery.getResultList();
        em.getTransaction().commit();
        em.close();
        for (Author author : result) {
            System.out.println(author.getLastName() + " " + author.getFirstName());
        }
        return result;

Then I gave Cannot use an EntityTransaction while using JTA. 然后,我给了使用JTA时不能使用EntityTransaction的权限。 So my question is how can I use hibernate Search with JTA? 所以我的问题是如何在JTA中使用休眠搜索?

You have jta-data-source configured (contrast to non-jta-data-source). 您已配置了jta-data-source(与非jta-data-source相比)。 So most likely authorFacade.getEntityManager() returns EntityManager that uses JTA-transactions. 因此,很有可能authorFacade.getEntityManager()返回使用JTA事务的EntityManager。 Now you have entity manager which plays with JTA transactions in your hand. 现在您有了实体管理器,可以处理JTA事务。 You pass it as argument to getFullTextEntityManager. 您将其作为参数传递给getFullTextEntityManager。 Likely ftem.getTransaction().begin() just passes call to your original (JTA) EntityManager. 可能ftem.getTransaction()。begin()只是将调用传递给原始(JTA)EntityManager。 Then you are in problems, because getTransaction is supposed to be used only when you use application managed transactions, and one EntityManager will not play with two types of transactions. 然后您就遇到了问题,因为应该只在使用应用程序管理的事务时才使用getTransaction,而一个EntityManager不会与两种类型的事务一起玩。

Your options are: 您的选择是:

  1. If you are happy with JTA transactions, just use them as you use them elsewhere. 如果您对JTA事务感到满意,请像在其他地方使用它们一样使用它们。 I cannot see anything special with using them with Hibernate Search. 将它们与Hibernate Search结合使用时,我看不到任何特别之处。 If you simply don't know JTA transactions (and do not want to learn them now) and want about same transaction behavior as in your code now, annotate bean method with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) and remove transaction handling from your code. 如果您根本不了解JTA事务(并且现在不想学习它们)并且想要与现在的代码中的事务行为相同,请使用@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)注释bean方法,并从代码中删除事务处理。
  2. Configure non-jta-data-source and use it. 配置非jta数据源并使用它。

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

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