簡體   English   中英

Hibernate + Spring SessionFactory配置

[英]Hibernate+Spring SessionFactory configuration

什么是配置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();
}
}

注意:

如果我使用sessionFactory.openStatelessSession(); 在會話配置中的DAO類hibernate5.LocalSessionFactoryBean中不會引起問題。

但關鍵是-我想使用sessionFactory.getCurrentSession(); 我該如何實現?

希望您在spring配置文件中啟用了事務支持。 如果不是,請使用<tx:annotation-driven>啟用它

並且,聲明transactionManager如下:

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

嘗試從ColorsService刪除@Transactional ,如下所示:

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

好,問題解決了!

在我的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)。 現在正在工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM