Hibernate + Spring SessionFactory配置

时间:2015-10-03 06:31:18

标签: java spring hibernate sessionfactory

配置SessionFactory的正确方法是什么?

如果我这样做:

<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" />

我收到此错误:

nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

如果我改为AnnotationSessionFactoryBean

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
             p:dataSource-ref="dataSource"
             p:packagesToScan="ua.com.javer.flowerexpert"/>

我明白了:

nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;

即使在一些较旧的项目hibernate3.annotation.AnnotationSessionFactoryBean中工作正常。

我的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>

这是我的服务类:

@Service("colorsService")
@Transactional
public class ColorsService {
@Autowired
private ColorDao colorDao;

public List<Color> getAllColors() {
    return colorDao.getAllColors();
}
}

这是 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();
}
}

注意:

如果我在会话配置中使用DAO类sessionFactory.openStatelessSession();中的hibernate5.LocalSessionFactoryBean不会导致问题。

但问题是 - 我想使用sessionFactory.getCurrentSession(); 我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

希望您在spring配置文件中启用transaction支持。如果没有,请使用<tx:annotation-driven>

启用它

另外,将transactionManager声明如下:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

尝试从@Transactional删除ColorsService,如下所示:

@Service("colorsService")
public class ColorsService {
@Autowired
private ColorDao colorDao;

public List<Color> getAllColors() {
    return colorDao.getAllColors();
}
}

并将其添加到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();
}
}

编辑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" />

答案 1 :(得分:0)

好的,问题解决了!

mvc-dispatcher-servlet.xml我已经:

    <context:component-scan base-package="ua.com.javer.flowerexpert" />

与此同时:

<context:component-scan base-package="ua.com.javer.flowerexpert.dao"/>
dao-context.xml

,因此ua.com.javer.flowerexpert.dao包被扫描了两次。

我已将更改的软件包更改为mvc-dispatcher-servlet.xml到:

    <context:component-scan base-package="ua.com.javer.flowerexpert.controller" />

仅扫描ua.com.javer.flowerexpert.controller包(而不是dao)。现在它正在发挥作用。