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:
- Create a Persistence.xml file
- 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:
- Delete a meta-inf/persistence.xml configuration file
- Statement Packagestos can properties
- 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