简体   繁体   中英

Hibernate+Spring SessionFactory configuration

What is the right way to configure 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 :

    <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.

My pom.xml contains:

        <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 :

@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(); in DAO class hibernate5.LocalSessionFactoryBean in session configuration would not cause a problem.

But the point is - I want to use sessionFactory.getCurrentSession(); How can I achieve this?

Hope you enabled the transaction support in your spring configuration file. If not, enable it using <tx:annotation-driven>

And also, declare the transactionManager as follows:

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

Try removing @Transactional from ColorsService as below:

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

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

And adding it to 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:

<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:

    <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.

I've changed packages to scan in mvc-dispatcher-servlet.xml to:

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

to scan ua.com.javer.flowerexpert.controller package only (and not dao). Now it is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM