Spring integrates JPA and springjpa
Although most of JPA development uses hibernate specifications, there is a significant difference in integration with spring. Let's look at the important configurations of spring integration with jpa.
<Context: annotation-config/> <bean id = "entityManagerFactory" class = "org. springframework. orm. jpa. localContainerEntityManagerFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <property name =" jpaVendorAdapter "> <bean class =" org. springframework. orm. jpa. vendor. hibernateJpaVendorAdapter "> <property name =" showSql "value =" false "/> </bean> </property> <property name =" jpaProperties "> <props> <prop k Ey = "hibernate. hbm2ddl. auto"> update </prop> </props> </property> </bean> <! -- Use the following configuration if no data source exists --> <! -- <Bean id = "entityManager" class = "org. springframework. orm. jpa. localEntityManagerFactoryBean "> <property name =" persistenceUnitName "value =" mengya "> </property> </bean> --> <bean class =" org. springframework. orm. jpa. support. persistenceAnnotationBeanPostProcessor "/> <bean id =" txManager "class =" org. springframework. orm. jpa. jpaTransactionManager "> <property name =" entityManagerFactory "ref =" entityManagerFactory "/> </bean> <tx: annotation-driven transaction-manager =" txManager "/>
See here I believe I don't need to explain more everyone should be able to understand, if you use this method to pay attention to src must exist under the META-INF folder and below should also put a persistence. xml. The content of this xml file is roughly as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Persistence 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" version = "1.0"> <! -- Random name attribute --> <persistence-unit name = "springmvc"/> </persistence>
The next step is the data source. I am used to using the dbcp data source configuration as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context ht Tp: // www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <bean id =" dataSource "class =" org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "value =" org. gjt. mm. mysql. driver "/> <property name =" url "value =" jdbc: mysql: // localhost: 3306/exercise? UseUnicode = true & characterEncoding = UTF-8 "/> <property name =" username "value =" root "/> <property name =" password "value =" 123456 "/> <! -- Initial value when the connection pool starts --> <property name = "initialSize" value = "1"/> <! -- Maximum value of the connection pool --> <property name = "maxActive" value = "500"/> <! -- Maximum idle value. after a peak time, the connection pool can slowly release a portion of connections that are no longer in use, until maxIdle --> <property name = "maxIdle" value = "2"/> <! -- Minimum idle value. when the number of idle connections is less than the threshold value, the connection pool will pre-apply for some connections, so that you do not have time to apply when the peak traffic arrives --> <property name = "minIdle" value = "1"/> </bean> </beans>
After spring integrates jpa, the corresponding dao writing will also change. Let's look at the two methods.
@PersistenceContext private EntityManager entityManager = null; @Transactional(propagation=Propagation.REQUIRED) public void save(User u) { entityManager.persist(u); } @Transactional(propagation=Propagation.REQUIRED) public void updateUser(User u) { entityManager.merge(u); }
Compared with hibernate, only sessionfactory is replaced with EntityManager. The corresponding crud operation uses entityManager to call the corresponding method. To facilitate debugging, paste the log Configuration
## Log4J Settings for log4j 1.2.x (via jakarta-commons-logging)## The five logging levels used by Log are (in order):## 1. DEBUG (the least serious)# 2. INFO# 3. WARN# 4. ERROR# 5. FATAL (the most serious)# Set root logger level to WARN and append to stdoutlog4j.rootLogger=DEBUG, stdout#info#DEBUG#ERRORlog4j.appender.stdout=org.apache.log4j.ConsoleAppender#log4j.appender.stdout=org.apache.log4j.FileAppender#log4j.appender.stdout.File=d:\\logs.log log4j.appender.stdout.layout=org.apache.log4j.PatternLayout# Pattern to output the caller's file name and line number.log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n# Print only messages of level ERROR or above in the package noModule.log4j.logger.noModule=ERROR# OpenSymphony Stufflog4j.logger.com.opensymphony=ERRORlog4j.logger.freemarker=ERRORlog4j.logger.org.hibernate.jdbc=DEBUG#org.hibernate.SQL #org.hibernate.type #org.hibernate.tool.hbm2ddl #org.hibernate.pretty #org.hibernate.cache #org.hibernate.transaction #org.hibernate.jdbc #org.hibernate.hql.AST #org.hibernate.secure# Spring Stufflog4j.logger.org.springframework=ERROR
An error occurred while integrating JPA with Spring. Let's take a look.
@ GeneratedValue (strategy = GenerationType. AUTO)
We recommend that you put it before the get method.
Does spring implement jpa by itself? Is it just the difference between integrating the good jpa implemented by others and hibernate (not its jpa part )?
Jpa is a persistence standard developed by sun. spring fully supports it, so it is not necessary for him to write another one.