同样的文件(persistence.xml)在不同时期(jpa和spring整合前,后),在不同地点(不同的测试方法中)中的内容是不一样的。
1:时间:jpa未和spring整合之前,地点在下面的测试方法当中:
/**
* jpa未和spring集成整合之前的测试方法,会扫描所有的实体类,,生成所有的数据表(标记为entity的类)
*/
public void test(){
Persistence.createEntityManagerFactory("itcast");
}
persistence.xml文件必须是:
<?xml version="1.0"?>
<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/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!-- file:///D:/hibernate/hibernate-entitymanager-3.3.2.CR1/resources/org/hibernate/ejb/persistence_1_0.xsd -->
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
其中的红色四行是必须有的,否则在运行方法:
public void test(){
Persistence.createEntityManagerFactory("itcast");
}
的时候,就会报
No suitable driver found for jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8的异常
或者是java.lang.UnsupportedOperationException: The user must supply a JDBC connection(根据这四条语句的先后顺序不同,会报不同错误)
/**
* jpa未和spring集成整合之前的测试方法,该方法将生成一条记录
*/ public void test1(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist(new ProductType());
em.getTransaction().commit();
em.close();
factory.close();
}
2:时间:jpa未和spring整合之后,地点在下面的测试方法当中:
/**
* jpa未和spring集成整合之后的测试方法
*/
public void test2(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");
ProductType type = new ProductType();
type.setName("瑜伽用品");
type.setNote("好产品");
productService.save(type);
}
Juni会报这样的错:org.springframework.transaction.CannotCreateTransactionException: Could not open JPAEntityManager for transaction; nested exception is java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:375)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy17.save(Unknown Source)
at junit.test.ProductTest.test2(ProductTest.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:899)
at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:44)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:38)
at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:70)
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:330)
... 23 more
而相应的MyEclipse控制台会报这样的错:
java.lang.UnsupportedOperationException: Not supported by BasicDataSource
这是因为在集成完spring以后,beans.xml里有这样的一句话:
<context:property-placeholder location="classpath:jdbc.properties"/>
而jdbc.properties:
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
initialSize=1
maxActive=100
maxIdle=8
minIdle=1
而jdbc.properties文件中的红色的4句和persistence.xml文件中的红色的四句是有冲突的,所以才出现了上面的错误,所以把persistence.xml文件中的红色的四句去掉就可以了。
总结:最大的收获就是同样的代码比如说A代码你测验过是好使的,但是假如你又写了代码B,那么即使代码A你从来没修改过,那么A也很有可能因为代码B出现的原因而不能运行了!