JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范。目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 Spring提供三种方法集成JPA: 1、LocalEntityManagerFactoryBean:适用于那些仅使用JPA进行数据访问的项目。该FactoryBean根据 JPA PersistenceProvider自动检测配置文件进行工作,一般从“META-INF/persistence.xml”读取配置信息。这种方式最简单,但是不能设置Spring中定义的DataSource,且不支持Spring管理的全局事务。不建议使用此方式。这种方法实际上只适用于独立的应用程序和测试环境(这正是JPA规范设计它的原因)。 在Spring中的配置: <bean id=”entityManagerFactory” class=”org.springframework.orm.jpa.LocalEntityManagerFactoryBean”> <property name=”persistenceUnitName” value=”persistenceUnit”/> </bean>
2、从JNDI中获取:用于从Java EE服务器中获取指定的EntityManagerFactory,这种方式在Spring事务管理时一般要使用JTA事务管理。 Spring中的配置: <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:jee="http://www./schema/jee" xsi:schemaLocation=" http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/jee http://www./schema/jee/spring-jee-3.0.xsd"> <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/persistenceUnit"/> </beans>
在标准的Java EE 5启动过程中,Java EE服务器自动检测持久化单元(例如应用程序文件包中的META-INF/persistence.xml) ,以及Java EE部署描述符中定义给那些持久化单元命名上下文位置的环境的persistence-unit-ref项(例如web.xml)。 在这种情况下,整个持久化单元部署,包括持久化类的织入(字码码转换)都取决于Java EE服务器。 JDBC DataSource 通过在META-INF/persistence.xml 文件中的JNDI位置进行定义;EntityManager事务与服务器的JTA子系统整合。Spring仅仅用获得的 EntityManagerFactory, 通过依赖注入将它传递给应用程序对象,并为它管理事务(一般通过JtaTransactionManager)。 注意,如果在同一个应用程序中使用了多个持久化单元,JNDI获取的这种持久化单元的bean名称 应该与应用程序用来引用它们的持久化单元名称相符(例如@PersistenceUnit和 @PersistenceContext注解)。 在部署到Java EE 5服务器时使用该方法。关于如何将自定义JPA提供者部署到服务器,以及允许使用服务器提供的缺省提供者之外的JPA提供者,请查看服务器文档的相关说明。
3、LocalContainerEntityManagerFactoryBean:适用于所有环境的FactoryBean,能全面控制EntityManagerFactory配置,非常适合那种需要细粒度定制的环境。 该bean有以下属性:
JPA配置实例: persistence.xml: <?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/persistencehttp://java./xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"/> </persistence> persistence.xml中,指定持久化单元名称和事务类型,其他在Spring中配置。 applicationContext.xml <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="persistenceXmlLocation" value="chapter8/persistence.xml"/> <property name="persistenceUnitName" value="persistenceUnit"/> <property name="persistenceProvider" ref="persistenceProvider"/> <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> <property name="jpaDialect" ref="jpaDialect"/> <property name="jpaProperties"> <props> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="persistenceProvider" class="org.hibernate.ejb.HibernatePersistence"/><bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="false" /> <property name="database" value="HSQL"/> </bean> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
JpaTemplate类: Spring提供JpaTemplate模板类用于简化事务管理及常见操作,类似于JdbcTemplate模板类。 |
|