Java Reference
In-Depth Information
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Now you can delete the Hibernate configuration file (i.e., hibernate.cfg.xml ) because its
configurations have been ported to Spring.
Configuring a JPA Entity Manager Factory in Spring
First of all, let's modify JpaCourseDao to accept an entity manager factory via dependency injection,
instead of creating it directly in the constructor.
package com.apress.springenterpriserecipes.course.jpa;
...
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JpaCourseDao implements CourseDao {
private EntityManagerFactory entityManagerFactory;
public void setEntityManagerFactory(
EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
...
}
The JPA specification defines how you should obtain an entity manager factory in Java SE and Java
EE environments. In a Java SE environment, an entity manager factory is created manually by calling the
createEntityManagerFactory() static method of the Persistence class.
Now let's create a bean configuration file for using JPA (e.g., beans-jpa.xml in the classpath root).
Spring provides a factory bean, LocalEntityManagerFactoryBean , for you to create an entity manager
factory in the IoC container. You must specify the persistence unit name defined in the JPA
configuration file. You can also declare a JpaCourseDao instance under Spring's management.
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa. LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="course" />
</bean>
Search WWH ::




Custom Search