[英]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: 您的选择是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.