Spring整合JPA,springjpa
雖然JPA開發大多使用hibernate規範來進行,但是和spring整合這一塊卻存在明顯的差異,先來看下spring整合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 key="hibernate.hbm2ddl.auto">update</prop></props> </property></bean><!-- 如果沒有資料來源可以使用如下配置 --><!-- <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" />
看到這裡相信不用我多解釋大家應該能明白了,如果使用這種方式要注意src下面必須存在META-INF檔案夾且下面還應該放一個persistence.xml,這個xml檔案內容大致如下
<?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"><!-- name屬性隨意--><persistence-unit name="springmvc" /></persistence>
接下來就是資料來源了,我習慣使用dbcp的資料來源配置大致如下:
<?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 http://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" /><!-- 串連池啟動時的初始值 --><property name="initialSize" value="1" /><!-- 串連池的最大值 --><property name="maxActive" value="500" /><!-- 最大空閑值.當經過一個高峰時間後,串連池可以慢慢將已經用不到的串連慢慢釋放一部分,一直減少到maxIdle為止 --><property name="maxIdle" value="2" /><!-- 最小空閑值.當閒置串連數少於閥值時,串連池就會預申請去一些串連,以免洪峰來時來不及申請 --><property name="minIdle" value="1" /> </bean> </beans>
spring整合了jpa之後相應的dao的寫法也會發生變化,來看下這兩個方法
@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); }
相比hibernate只是把sessionfactory換成了EntityManager,相應的crud操作就使用entityManager調用相應的方法就OK了。為了調試方便順道把日誌的配置貼出來
## 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
Spring整合JPA出錯,老大們幫忙看看
@GeneratedValue(strategy=GenerationType.AUTO)
建議你放在 get方法之前。
spring 他自己實現了jpa 難道只是整合別人實現好的 jpa與hibernate(不是他的jpa部分)的不同
jpa是sun公司制定的持久化標準,spring完全支援它,所以他沒必要自己再寫一個。