Spring Integrated JPA Improvement approach

Source: Internet
Author: User

In standard JPA, the persistence unit is defined by default in the Meta-inf/persistence.xml file and is obtained through the @entity annotation search. However, in most cases, the persistence unit will not only have one, and the data source will not be one, for this reason, Spring provides an optional solution, This is managed through Localentitymanagerfactorybean and Localcontainerentitymanagerfactorybean. Localentitymanagerfactorybean expansion function Too little, this does not say, we take localcontainerentitymanagerfactorybean as an example.

Our general practice is to:

    1. Create a Persistence.xml file
    2. Configuring the Spring context
<persistence xmlns= "http://java.sun.com/xml/ns/persistence" version= "1.0" ><persistence-unit name= " Hibernatepersistenceunit "transaction-type=" resource_local "><properties><property name=" Hibernate.hbm2ddl.auto "value=" Update "/><property name=" Hibernate.dialect "value=" Org.hibernate.dialect.MySQL5InnoDBDialect "/></properties></persistence-unit></persistence >

then introduce the Persistence.xml configuration file in spring,

<bean id= "Entitymanagerfactory" class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" P: persistencexmllocation= "Classpath*:meta-inf/persistence.xml" p:persistenceunitname= "HibernatePersistenceUnit" P: datasource-ref= "Jpadatasource" p:jpavendoradapter-ref= "Hibernatevendor"/> <bean id= "HibernateVendor" class= " Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter "p:showsql=" false "/>
How to Improve

Based on the documentation requirements, we want to remove the Persistence.xml file, but how do we declare the external configuration file to be associated with the ORM framework? And how to get the Entitymanagerfactory factory class to know where we configured the entity.

Can do this:

    1. Delete a meta-inf/persistence.xml configuration file
    2. Statement Packagestos can properties
    3. Statement Jpapropertymap Property

The final configuration is as follows:

<bean id= "Entitymanagerfactory" class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" P: Packagestoscan= "Org.krams.tutorial.domain" p:datasource-ref= "Jpadatasource" p:jpavendoradapter-ref= " Hibernatevendor "p:jpapropertymap-ref=" Jpapropertymap "/> <util:map id=" Jpapropertymap ">    <entry Key = "Hibernate.hbm2ddl.auto" value= "Update"/>    <entry key= "Hibernate.dialect" value= " Org.hibernate.dialect.MySQL5InnoDBDialect "/></util:map> <bean id=" Hibernatevendor "class=" Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter "p:showsql=" false "/>

The advantage of this is that when you really need to invoke the corresponding entity object and then configure the data source, let spring automatically manage the factory so that the data source is not afraid of conflicts.



Spring Integrated JPA Improvement approach

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.