Java Reference
In-Depth Information
You can test this DAO with the similar Main class, but this time you instantiate the JPA DAO
implementation instead.
package com.apress.springenterpriserecipes.course;
...
public class Main {
public static void main(String[] args) {
CourseDao courseDao = new JpaCourseDao();
...
}
}
In the preceding DAO implementations for both Hibernate and JPA, there are only one or two lines
that are different for each DAO method. The rest of the lines are boilerplate routine tasks that you have
to repeat. Moreover, each ORM framework has its own API for local transaction management.
3-8. Configuring ORM Resource Factories in Spring
Problem
When using an ORM framework on its own, you have to configure its resource factory with its API.
For Hibernate and JPA, you have to build a session factory and an entity manager factory from the
native Hibernate API and JPA. You have no choice but to manage these objects manually, without
Spring's support.
Solution
Spring provides several factory beans for you to create a Hibernate session factory or a JPA entity
manager factory as a singleton bean in the IoC container. These factories can be shared between
multiple beans via dependency injection. Moreover, this allows the session factory and the entity
manager factory to integrate with other Spring data access facilities, such as data sources and
transaction managers.
How It Works
Configuring a Hibernate Session Factory in Spring
First of all, let's modify HibernateCourseDao to accept a session factory via dependency injection, instead
of creating it directly with the native Hibernate API in the constructor.
package com.apress.springenterpriserecipes.course.hibernate;
...
import org.hibernate.SessionFactory;
public class HibernateCourseDao implements CourseDao {
private SessionFactory sessionFactory;
 
Search WWH ::




Custom Search