Spring: integrate JPA in Spring;

Source: Internet
Author: User

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 }...}

 
  
  
  





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.