Integrating Spring and JPA

Source: Internet
Author: User
The second way to integrate spring and JPA is to get entitymanagerfactory by using Springframe's Localcontainerentitymanagerfactorybean.

Change the class reference for the previous fetch entitymanagerfactory:

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

All we need to do is 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 flexible local configuration in the application. It supports connecting existing JDBC data sources, supporting local transactions (resource_local) and global Transactions (JTA).

In addition, can also be used with DataSource, our persistence.xml without writing the properties of the connection, directly to write the name and 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/persistence http://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= "1000"/>
</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= "false"/>
<property name= "Generateddl" value= "false"/>
</bean>
</property>
</bean>

Third-party approach is to use Jee:jndi-lookup to get entitymanagerfactory from Jndi

<bean>

<jee:jndi-lookup id= "Entitymanagerfactory" jndi-name= "Persistence/mypersistenceunit"/>

</bean>

In the business logic layer we have to use the @transactional annotation identifier, otherwise the method will not commit the transaction.

For spring transaction propagation and transaction isolation see:

hi.baidu.com/coolcat%5fpolice/blog/item/59ebc12324be8d4b935807d4.html

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.