一,整合说明
承接上一片文章:[hibernate4实现的JPA2.0规范的集成] 这里讲述一下我集成过程中遇到的一些问题,以及解决方法
二,加入Spring支持
在spring的官网下载工程需要的jar包,官网有时候也有链接失败的情况(spring之前的是springframework.org,现在似乎换了链接http://spring.io/,在这里慢慢去找吧!
1. 在原来的项目的基础上,新增以下jar包,截图如下
2. 在src目录下新建spring-*.xml文件,自己随意命名,也可以拷贝其他文件的模板,极简配置代码如下,同时添加jdbc.properties文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd" >
<!-- 注解支持 -->
<context:annotation-config />
<context:component-scan base-package="com.zjmusic" /><!-- 扫描业务逻辑层和模型层 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<task:annotation-driven/>
<!-- 数据源配置 -->
<bean id="mydataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mydataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
<property name="jpaProperties">
<props>
<prop key="hibernate.connection.driver_class">
${jdbc.driverClass}
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">18</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
jdbc.properties如下:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:1949/DB_ZJMusic
jdbc.user=root
jdbc.password=root
三,编写测试类
public class SpringJpaTest {
private static ApplicationContext cxt;
private static JMusicService jmService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//WebRoot/WEB-INF/
cxt= new ClassPathXmlApplicationContext("spring-mvc.xml");
jmService = (JMusicService) cxt.getBean("jMusicServiceBean");
}
@Test
public void testsave() {
for(int i = 0;i < 15;i++) {
JMusic jm = new JMusic();
jm.setAlbum("BigBun");
jm.setDistributeDate(Timestamp.valueOf(DateUtil.getNowDate()));
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
jm.setMusicname(uuid.substring(3, 10));
jm.setMusicurl("http://www.baidu.com/music/20150308/"+uuid.substring(3, 10)+".mp3");
jm.setSinger("Johns Brooks");
jmService.save(jm);
}
}
}
四,异常总结
1,BeanCreationException:Error creating bean with name 'jMusicServiceBean': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emf' defined in class path resource [spring-mvc.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError。
2,SAXParseException; lineNumber: 14; columnNumber: 121; schema_reference.4: 无法读取方案文档 'http://www.springframework.org/schema/beans/spring-beans-3.2.xsd', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>
异常基本会出现上面的两种,第一个BeanCreationException有多种可能性
--如果配置有问题,那么在异常中会提示的很清楚,很容易解决。无非是:a,没有提供provider<provider>org.hibernate.ejb.HibernatePersistence</provider> b,验证模式写成none<property name="javax.persistence.validation.mode" value="none"/> c,还有可能是数据库至少提供一种方言的支持。
--如果配置严格按照我上面的来写,还出现了BeanCreationException,那最大的可能性就是jar的冲突了,这种情况如果是copy别人的不同版本的jar包,很难排查出,自己手动一个一个jar包添加可以说是一种笨而又直接的解决办法,同时还可以知道那些jar包有哪些作用。我这里就是spring的核心包与j2ee的包产生了冲突.解决办法是将j2ee重新打包,删除其中的javaee.jar,同时加上servlet-api.jar,重新打包就好了
第二种异常:SAXParseException,是因为拷贝别人的spring配置文件导致的,首先看看你的spring.schema(在spring的beans对应的jar包下的META-INF文件夹下)文件中支持的spring的版本,我之前是写的3.2版本,结果打开方案文件,里面却最高支持3.1,改成3.2就好了