我试图让当前会话处于休眠状态以创建条件查询,但我收到了错误。
这是我的代码:
datasource.xml:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" ref="persistenceUnit" />
<property name="jpaProperties" ref="jpaProperties" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="globalRollbackOnParticipationFailure" value="true" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" ref="sqlDialect" />
</bean>
class java:
@Autowired
private SessionFactory sessionFactory;
Session session = sessionFactory.getCurrentSession();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
List<Product> bookResult = new ArrayList<>();
try {
// This will ensure that index for already inserted data is created.
fullTextEntityManager.createIndexer().startAndWait();
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Product.class).get();
org.apache.lucene.search.Query query = qb.keyword().onFields("name").matching("cocacola").createQuery();
javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(query, Product.class);
Criteria criteria = session.createCriteria(Product.class);
criteria.add(Restrictions.eq("category.supermarket", "mercadona"));
List<Product> productos = criteria.list();
但是得到这个错误:
org.hibernate.HibernateException:找不到当前线程的会话 在 org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
我该如何解决这个问题?
答案 0 :(得分:0)
您是否已添加到servlet-context.xml
<tx:annotation-driven transaction-manager="transactionManager" />
使用@Transaction?
并检查@Autowired是否在您的class.java中以System.out.println(sessionFactory)
为例正确运行,以确保它不为空。