欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Could not open JPA EntityManager for transaction

程序员文章站 2022-07-13 16:13:59
...
Spring2.5+JPA+Struts1.3 整合开发出现了:Could not open JPA EntityManager for transaction;

总结了一下几点
1.数据库方言的问题 下列是各数据库方言
DB2  org.hibernate.dialect.DB2Dialect 
DB2 AS/400  org.hibernate.dialect.DB2400Dialect 
DB2 OS390  org.hibernate.dialect.DB2390Dialect 
PostgreSQL  org.hibernate.dialect.PostgreSQLDialect 
MySQL  org.hibernate.dialect.MySQLDialect 
MySQL with InnoDB  org.hibernate.dialect.MySQLInnoDBDialect 
MySQL with MyISAM  org.hibernate.dialect.MySQLMyISAMDialect 
Oracle (any version)  org.hibernate.dialect.OracleDialect 
Oracle 9i/10g  org.hibernate.dialect.Oracle9Dialect 
Sybase  org.hibernate.dialect.SybaseDialect 
Sybase Anywhere  org.hibernate.dialect.SybaseAnywhereDialect 
Microsoft SQL Server  org.hibernate.dialect.SQLServerDialect 
SAP DB  org.hibernate.dialect.SAPDBDialect 
Informix  org.hibernate.dialect.InformixDialect 
HypersonicSQL  org.hibernate.dialect.HSQLDialect 
Ingres  org.hibernate.dialect.IngresDialect 
Progress  org.hibernate.dialect.ProgressDialect 
Mckoi SQL  org.hibernate.dialect.MckoiDialect 
Interbase  org.hibernate.dialect.InterbaseDialect 
Pointbase  org.hibernate.dialect.PointbaseDialect 
FrontBase  org.hibernate.dialect.FrontbaseDialect 
Firebird  org.hibernate.dialect.FirebirdDialect

2.导入的jar包可能错误################################
dist\spring.jar  
dist\modules\spring-webmvc-struts.jar  
lib\jakarta-commons\commons-logging.jar、commons-dbcp.jar、commons-pool.jar  
lib\aspectj\aspectjweaver.jar、aspectjrt.jar  
lib\cglib\cglib-nodep-2.1_3.jar  
lib\j2ee\common-annotations.jar  
lib\log4j\log4j-1.2.15.jar  
 
加入jpa的hibernate的实现包  
 
这里JPA的实现采用hibernate,需要使用到下面的jar文件  
 
Hiberante核心包(8个文件)  
hibernate-distribution-3.3.1.GA  
---------------------------------------------  
hibernate3.jar  
lib\bytecode\cglib\hibernate-cglib-repack-2.1_3.jar  
lib\required\*.jar  
 
Hiberante注解包(3个文件):hibernate-annotations-3.4.0.GA  
------------------------------------------------------------------------------------  
hibernate-annotations.jar  
lib\ejb3-persistence.jar、hibernate-commons-annotations.jar  
 
Hibernate针对JPA的实现包(3个文件):hibernate-entitymanager-3.4.0.GA  
------------------------------------------------------------------------------------------------------  
hibernate-entitymanager.jar  
lib\test\log4j.jar、slf4j-log4j12.jar  

3.注意persistence.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">  
    <persistence-unit name="xxx" transaction-type="RESOURCE_LOCAL">  
      <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="root"/>  
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>  
         <property name="hibernate.max_fetch_depth" value="3"/>  
         <property name="hibernate.hbm2ddl.auto" value="update"/>  
      </properties>  
  </persistence-unit>  
</persistence>

4.applicationContext.xml##################
<?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:aop="http://www.springframework.org/schema/aop" 
       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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    <!-- 启动注解依赖注入 -->  
     <context:annotation-config/>  
     <!-- 作用:跟hibernate的sessionFactory的作用是一样的,是一个连接工厂的对象,使用的是spring的jap的连接工厂 -->  
     <!-- persistenceUnitName 持久化单元名称 这个是要在src的META-INF配置 -->  
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">  
      <property name="persistenceUnitName" value="xxx"/> <!-- 这里的value="xxx"是persistence.xml的   name="xxx" -->

</bean>  
  <!-- 配置jpa的事务管理容器 -->  
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
    <property name="entityManagerFactory" ref="entityManagerFactory"/>  
  </bean>  
  <!-- 开启注解方式启动事务管理 -->  
<tx:annotation-driven transaction-manager="txManager"/>