1. Use the application management type JPA:
The following is a typical persistence. xml-persistence unit.
com.habuma.spitter.domain.Spitter
com.habuma.spitter.domain.Spittle
2. Use JPA of the container management type: <常用的方式>
Multiple JPA providers provided by Spring
EclipseLinkJpaVendorAdapter
HibernateJpaVendorAdapter
OpenJpaVendorAdapter
TopLinkJpaVendorAdapter
IBM DB2 DB2Apache Derby DERBYH2 H2Hypersonic HSQLINFORMIX INFORMIXMYSQL MYSQLOracle ORACLEPostgresQL POSTGRESQLMicrosoft SQL Server SQLSERVERSybase SYBASE
If the EntityManagerFactory has been created on the server, you can find it through JNDI:
Compile a JPA-based DAO:
Spring also provides JpaTemplate templates for jpa integration and corresponding dao support for JpaDaoSupport. However, to provide a more pure JPA method, template-based JPA has been discarded.
Since the pure JPA method is far better than the template-based JPA method, this chapter focuses on how to build a jpa dao that does not use Spring.
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") // catch exceptions @ Transactional // transaction management 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 // inject EntityManager emprivate 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); // update }...}