开发者

Hibernate search and JPA with JTA transaction

I want to use the Hibernate Search full text search capabilities. I have a simple Java EE application. I annotated the entity classes and here is my persistence.xml:

<persistence-unit name="library">
    <jta-data-source>jdbc/webrarydb</jta-data-source>
    <class>net.hcpeter.webrary.entities.Author</class>
   开发者_如何学Python <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. So my question is how can I use hibernate Search with JTA?


You have jta-data-source configured (contrast to non-jta-data-source). So most likely authorFacade.getEntityManager() returns EntityManager that uses JTA-transactions. Now you have entity manager which plays with JTA transactions in your hand. You pass it as argument to getFullTextEntityManager. Likely ftem.getTransaction().begin() just passes call to your original (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.

Your options are:

  1. If you are happy with JTA transactions, just use them as you use them elsewhere. I cannot see anything special with using them with 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.
  2. Configure non-jta-data-source and use it.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜