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