Spring JPA Configuration Explained

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"/>

</bean>

2. Get from Jndi: Used to get the specified entitymanagerfactory from the Java EE server, which is generally used for JTA transaction management in spring transaction management.

The configuration in spring:

<beans XMLns= "Http://www.springframework.org/schema/beans"    xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "    xmlns:jee=" Http://www.springframework.org/schema/jee "    xsi:schemalocation= "       Http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd       Http://www.springframework.org/schema/jee       http://www.springframework.org/schema/ Jee/spring-jee-3.0.xsd ">  <jee:jndi-look up id=" entitymanagerfactory "  jndi-name=" Persistence/persistenceunit "/></beans>

During the standard Java EE 5 boot process, the Java EE server automatically detects the persistence unit (for example, Meta-inf/persistence.xml in the application package), and the Java An PERSISTENCE-UNIT-REF item (for example, Web. xml) that is defined in the EE deployment descriptor for the environment where the persistence unit naming context is located.

In this case, the entire persistence unit deployment, including the persistence class's weaving (loadline code conversion), depends on the Java EE server. The JDBC DataSource is defined by the Jndi location in the Meta-inf/persistence.xml file, and the Entitymanager transaction is integrated with the JTA subsystem of the server. Spring simply passes the obtained entitymanagerfactory, passing it to the Application object through dependency injection, and manages the transaction for it (typically through Jtatransactionmanager).

Note that if more than one persistence unit is used in the same application, the bean name of the persisted unit that Jndi obtains should match the name of the persistence unit that the application uses to reference them (for example, @persistenceunit and @ Persistencecontext annotations).

This method is used when deploying to a Java EE 5 server. For instructions on how to deploy a custom JPA provider to a server, and to allow a JPA provider other than the default provider provided by the server, review the documentation for your server.

3, Localcontainerentitymanagerfactorybean: Suitable for all environments Factorybean, can fully control the Entitymanagerfactory configuration, very suitable for the kind of the need for fine-grained custom environment.

The bean has the following properties:

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.

JPA Configuration instance:

Persistence.xml:

<?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/persistence                      
    Http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd ">    <persistence-unit name=" Persistenceunit " Transaction-type= "Resource_local"/></persistence>
  Persistence.xml, specify the persistence unit name and transaction type, and others are configured in spring.
  Applicationcontext.xml
 <bean id= "entitymanagerfactory" class= " Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "> <property name=" DataSource "ref=" DataSource "/> <property name=" persistencexmllocation "value=" Chapter8/persistence.xml "/> <property name = "Persistenceunitname" value= "Persistenceunit"/> <property name= "Persistenceprovider" ref= " Persistenceprovider "/> <property name=" Jpavendoradapter "ref=" Jpavendoradapter "/> <property name=" jpaDi Alect "ref=" Jpadialect "/> <property name=" jpaproperties "> <props> <prop key=" Hibern Ate.show_sql ">true</prop> </props> </property> </bean> <bean id=" Persistenceprov Ider "class=" org.hibernate.ejb.HibernatePersistence "/> 
 <bean id= "Jpavendoradapter" class= "Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" > < Property Name= "Generateddl" value= "Fals  E"/> <property name= "Database" value= "HSQL"/> </bean> <bean id= "Jpadialect" class= " Org.springframework.orm.jpa.vendor.HibernateJpaDialect "/> 
    • Localcontainerentitymanagerfactorybean: Specifies the use of local container management entitymanagerfactory for fine-grained control;
    • The DataSource property specifies the data source defined using Spring;
    • persistencexmllocation Specifies that the JPA configuration file is Chapter8/persistence.xml, and that the configuration file is very simple and is fully configured in spring;
    • persistenceunitname Specifies the persistence unit name, which is specified in the JPA configuration file;
    • Persistenceprovider: Specifies the JPA persistence provider, where Hibernate is used to implement the Hibernatepersistence class;
    • jpavendoradapter: Specifies the implementation of the vendor-specific feature, that is, generateddl= false means that ddl,database= Hsql is not automatically generated to indicate the use of HSQLDB database;
    • jpadialect: If you specify Jpavendoradapter this attribute is optional, here is hibernatejpadialect;
    • jpaproperties: Specifying "Hibernate.show_sql =true" here means that all generated SQL will be printed at the debug level of the log system.

Jpatemplate class:

Spring provides jpatemplate template classes for simplifying transaction management and common operations, similar to the JdbcTemplate template class.

Reference:

[1] http://www.cnblogs.com/jacksun1978/archive/2012/04/11/2443096.html

Spring JPA Configuration Explained

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.