以下是典型的persistence.xml---持久化单元
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="spitterPU"> <class>com.habuma.spitter.domain.Spitter</class> <class>com.habuma.spitter.domain.Spittle</class> <properties> <propertyname="toplink.jdbc.driver" value="org.hsqldb.jdbcDriver"/> <propertyname="toplink.jdbc.url" value= "jdbc:hsqldb:hsql://localhost/spitter/spitter"/> <propertyname="toplink.jdbc.user" value="sa" /> <propertyname="toplink.jdbc.password" value="" /> </properties> </persistence-unit> </persistence> <bean id="emf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <propertyname="persistenceUnitName"value="spitterPU"/> </bean>
2、使用容器管理类型的JPA: <常用的方式>
<bean id="emf"class= "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <propertyname="dataSource"ref="dataSource"/><!-- 引用数据源--> <propertyname="jpaVendorAdapter"ref="jpaVendorAdapter"/><!-- jpa提供商--> </bean>
下是Spring提供的多个JPA提供商
EclipseLinkJpaVendorAdapter
HibernateJpaVendorAdapter
OpenJpaVendorAdapter
TopLinkJpaVendorAdapter
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <propertyname="database"value="HSQL"/><!--指明那种数据库,下面提供了多种数据库类型--> <propertyname="showSql"value="true"/> <propertyname="generateDdl"value="false"/> <propertyname="databasePlatform" value="org.hibernate.dialect.HSQLDialect"/> </bean>
IBM DB2 DB2 Apache Derby DERBY H2 H2 Hypersonic HSQL INFORMIX INFORMIX MYSQL MYSQL Oracle ORACLE PostgresQL POSTGRESQL Microsoft SQL Server SQLSERVER Sybase SYBASE
如果服务器中已经创建好了EntityManagerFactory,可以通过JNDI查找: <jee:jndi-lookupid="emf"jndi-name="persistence/spitterPU"/>
编写基于JPA的DAO:
Spring也对jpa集成提供了JpaTemplate模板,以及对应的dao支持JpaDaoSupport,但是为了提供更纯粹的JPA方式,基于模板的JPA已经弃用了。
鉴于纯粹的JPA方式远胜于基于模板的JPA,所以本章重点关注如何构建不使用Spring的JPA DAO。
import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import com.habuma.spitter.domain.Spitter; import com.habuma.spitter.domain.Spittle; @Repository("spitterDao") //捕获异常 @Transactional //事物管理 public classJpaSpitterDaoimplementsSpitterDao{ private static final String RECENT_SPITTLES= "SELECTsFROMSpittles"; private static final String ALL_SPITTERS= "SELECTsFROMSpitters"; private static final String SPITTER_FOR_USERNAME= "SELECTsFROMSpittersWHEREs.username=:username"; private static final String SPITTLES_BY_USERNAME= "SELECTsFROMSpittlesWHEREs.spitter.username=:username"; @PersistenceContext //注入EntityManager em private EntityManager em; public voidaddSpitter(Spitter spitter){ em.persist(spitter); } public SpittergetSpitterById(long id){ returnem.find(Spitter.class,id); } public voidsaveSpitter(Spitter spitter){ em.merge(spitter); //更新 } ... }
<!-- 以下两个bean要装配到spring中去--> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>