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"/>
</bean>
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"/>
</bean>
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= "http://java.sun.com/xml/ns/persistence" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/ Persistence/persistence_1_0.xsd ">
<persistence-unit name= "Mypu" transaction-type= "resource_local" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>myentiry. Userinfo</class>
</persistence-unit>
</persistence>
Spring's applicationcontext configuration file:
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name= "url" value= "Jdbc:sqlserver://192.168.2.44:1433;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>
<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"/>
</bean>
</property>
</bean>
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.xml:
<persistence version= "1.0"
Xmlns= "Http://java.sun.com/xml/ns/persistence"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/persistence
Http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd ">
<persistence-unit name= "Persistenceunit" transaction-type= "resource_local"/>
</persistence>
Three ways to integrate JAP and spring