Three ways to integrate JAP and spring

Source: Internet
Author: User

JPA is one of the Java EE5 specifications and is an ORM specification that is implemented by the vendor. Currently there are hibernate,openjpa,toplink and ECLIPSEJPA and other implementations

Spring provides three ways of integrating JPA:

1. Localentitymanagerfactorybean: Applies to projects that use JPA for data access only. The Factorybean works according to the JPA Persistenceprovider Auto-detect configuration file and generally reads the configuration information from "Meta-inf/persistence.xml". This is the simplest approach, but you cannot set the DataSource defined in spring and do not support spring-managed global transactions. This method is not recommended. This approach actually applies only to stand-alone applications and test environments (which is why the JPA specification designed it).

Configuration in spring:

<bean id= "Entitymanagerfactory" class= "Org.springframework.orm.jpa.LocalEntityManagerFactoryBean" >

<property name= "Persistenceunitname" value= "Persistenceunit"/>


The way to get Entitymanagerfactory is to use Springframe's Localcontainerentitymanagerfactorybean.

Change the class reference to get Entitymanagerfactory earlier:

<bean id= "Entitymanagerfactory" class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name= "persistencexmllocation" value= "Classpath:meta-inf/persistence.xml"/>

We only need to configure persistencexmllocation this property to specify the Meta-inf/persistence.xml inside the classpath.

This approach is a powerful way to configure JPA. Because it allows for the flexibility of local configuration in the application. It supports connecting to existing JDBC data sources, supporting local transactions (resource_local) and global Transactions (JTA).

In addition, it can be used in conjunction with DataSource, our persistence.xml does not have to write the properties of the connection, directly write the name and the corresponding entity on it, for example:

<?xml version= "1.0" encoding= "UTF-8"?>
<persistence version= "1.0" xmlns= "" xmlns:xsi= " Xmlschema-instance "xsi:schemalocation=" Persistence/persistence_1_0.xsd ">
<persistence-unit name= "Mypu" transaction-type= "resource_local" >

<class>myentiry. Userinfo</class>



Spring's applicationcontext configuration file:

<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= ""/>
<property name= "url" value= "Jdbc:sqlserver://;databasename=test"/>
<property name= "username" value= "sa"/>
<property name= "password" value= "sa"/>
<property name= "InitialSize" value= "5"/>
<property name= "Minidle" value= "5"/>
<property name= "Maxidle" value= "/>"
<property name= "maxactive" value= "/>"
<property name= "maxwait" value= "/>"

<bean id= "Entitymanagerfactory"
class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>

<property name= "persistencexmllocation" value= "Classpath:meta-inf/persistence.xml"/>
<property name= "Persistenceunitname" value= "Mypu"/>
<property name= "Jpavendoradapter" >
<bean class= "Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
<property name= "Showsql" value= "true"/>
<property name= "Generateddl" value= "false"/>

Persistenceunitmanager: Used to obtain the JPA persistence unit, the default implementation of Defaultpersistenceunitmanager is used to resolve multiple configuration files.

DataSource: Used to specify the data source defined by spring.

Persistencexmllocation: For specifying JPA profiles, for multi-JPA profiles, select the Set Persistenceunitmanager property to resolve.

Persistenceunitname: Used to specify the name of the persisted unit.

Persistenceprovider: Used to specify the persistence implementation of the vendor class, such as Hibernate: Org.hibernate.ejb.HibernateProvider class.

Jpavendoradapter: Used to set the specific properties of the JPA implementation vendor, such as setting hibernate to automatically generate DDL properties GENERATEDDL, which are vendor-specific, so it's best to set up here. Currently spring provides four implementations of Hibernatejpavendoradapter,openjpavendoradapter,eclipsejpavendoradapter,toplinkjpavenderadapter. The most important attribute is "database", which is used to specify the type of database used. This determines how database-specific exceptions are converted to spring conformance exceptions, depending on the database type. The following databases are currently supported: Db2,derby,h2,hsql,informix,mysql,oracle,postgresql,sql_server,sybase

Jpadialect: Used to specify some advanced features, such as transaction management. Currently spring provides Hibernatejpadialect,openjpadialect,eclipsejpadialect,toplinkjpadialect and Defaultjpadialect implementations. Note that Defaultjpadialect does not provide any functionality, so you need to specify the Jpadialect implementation when using a specific implementation vendor's JPA implementation, such as using Hibernate with Hibernatejpadialect. When you specify the Jpavendoradapter property, you can specify no jpadialect, and the corresponding Jpadialect implementation is set automatically;

Jpaproperties and Jpapropertymap: Specify JPA properties, such as whether the "Hibernate.show_sql" property of SQL is displayed in Hibernate, The properties set for Jpaproperties are automatically placed into the Jpapropertymap;

Loadtimeweaver: Used to specify the Loadtimeweaver implementation, allowing JPA to modify the corresponding class file when loading. The specific JPA specification is used to implement vendor documentation, such as Hibernate, without the need to specify Loadtimeweaver.

You can also use JPA provisioning attribute correlation to specifically fetch the JPA profile persistence.xml:


<persistence version= "1.0"
Xmlns= "Http://"
Xmlns:xsi= "Http://"
Xsi:schemalocation= "Http://

Http:// ">
<persistence-unit name= "Persistenceunit" transaction-type= "resource_local"/>

Three ways to integrate JAP and spring

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