[英]Hibernate+Spring SessionFactory configuration
What is the right way to configure SessionFactory? 什么是配置SessionFactory的正确方法?
If I do it this way: 如果我这样做:
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="ua.com.javer.flowerexpert"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
I get this error: 我收到此错误:
nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
And if I change to AnnotationSessionFactoryBean
: 如果我更改为
AnnotationSessionFactoryBean
:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="ua.com.javer.flowerexpert"/>
I get: 我得到:
nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
Even though in some older project hibernate3.annotation.AnnotationSessionFactoryBean
works fine. 即使在某些较旧的项目中,
hibernate3.annotation.AnnotationSessionFactoryBean
可以正常工作。
My pom.xml
contains: 我的
pom.xml
包含:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.1.Final</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
Here's my Service class: 这是我的服务班级:
@Service("colorsService")
@Transactional
public class ColorsService {
@Autowired
private ColorDao colorDao;
public List<Color> getAllColors() {
return colorDao.getAllColors();
}
}
And here's the DAO : 这是DAO :
@Component
@Repository("colorDao")
public class ColorDaoHibernate implements ColorDao {
@Autowired
private SessionFactory sessionFactory;
public ColorDaoHibernate() {
}
@Override
public List<Color> getAllColors() {
Session session = sessionFactory.getCurrentSession();
// StatelessSession session = sessionFactory.openStatelessSession();
Query query = session.createQuery("FROM Color");
return query.list();
}
}
NOTICE: 注意:
If I use sessionFactory.openStatelessSession();
如果我使用
sessionFactory.openStatelessSession();
in DAO class hibernate5.LocalSessionFactoryBean
in session configuration would not cause a problem. 在会话配置中的DAO类
hibernate5.LocalSessionFactoryBean
中不会引起问题。
But the point is - I want to use sessionFactory.getCurrentSession();
但关键是-我想使用
sessionFactory.getCurrentSession();
How can I achieve this? 我该如何实现?
Hope you enabled the transaction support in your spring configuration file. 希望您在spring配置文件中启用了事务支持。 If not, enable it using
<tx:annotation-driven>
如果不是,请使用
<tx:annotation-driven>
启用它
And also, declare the transactionManager
as follows: 并且,声明
transactionManager
如下:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
Try removing @Transactional
from ColorsService
as below: 尝试从
ColorsService
删除@Transactional
,如下所示:
@Service("colorsService")
public class ColorsService {
@Autowired
private ColorDao colorDao;
public List<Color> getAllColors() {
return colorDao.getAllColors();
}
}
And adding it to ColorDaoHibernate
: 并将其添加到
ColorDaoHibernate
:
@Repository("colorDao")
public class ColorDaoHibernate implements ColorDao {
@Autowired
private SessionFactory sessionFactory;
public ColorDaoHibernate() {
}
@Transactional
@Override
public List<Color> getAllColors() {
Session session = sessionFactory.getCurrentSession();
// StatelessSession session = sessionFactory.openStatelessSession();
Query query = session.createQuery("FROM Color");
return query.list();
}
}
Edit sessionFactory
bean definition as below: 编辑
sessionFactory
bean定义,如下所示:
<bean id="hibernateProps"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:packagesToScan="ua.com.javer.flowerexpert"
p:hibernateProperties-ref="hibernateProps" />
OK, problem solved! 好,问题解决了!
In my mvc-dispatcher-servlet.xml
I've had: 在我的
mvc-dispatcher-servlet.xml
我有:
<context:component-scan base-package="ua.com.javer.flowerexpert" />
At the same time I had: 同时我有:
<context:component-scan base-package="ua.com.javer.flowerexpert.dao"/>
in dao-context.xml
, so the ua.com.javer.flowerexpert.dao
package was scanned twice. 在
dao-context.xml
,因此ua.com.javer.flowerexpert.dao
软件包被扫描了两次。
I've changed packages to scan in mvc-dispatcher-servlet.xml
to: 我将要在
mvc-dispatcher-servlet.xml
扫描的软件包更改为:
<context:component-scan base-package="ua.com.javer.flowerexpert.controller" />
to scan ua.com.javer.flowerexpert.controller
package only (and not dao). 仅扫描
ua.com.javer.flowerexpert.controller
程序包(而不扫描dao)。 Now it is working. 现在正在工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.