Spring+Jpa整合的过程中遇到的一个问题。。。纠结了我半天。。。

今天在出来Spring整合Jpa,由于是刚开始整合,难免会遇到很多问题。。。。。但是今天的这个问题真的让人很郁闷。。一直提示 No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either这个错误,旁边还显示着org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [beans.xml]:: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either 这样总的错误。。。

 到网上搜了好多好多方法和案例,最后终于找到了解决的方法。。我出现的问题主要是在

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="itcast" />
</bean>

这一块上,运行时就说这个entityManagerFactory错误。。。开始以为是class类指定的错误,网上也有一些方法说是吧LocalContainerEntityManagerFactoryBean这个类的Container这个部分去掉,我试过。。结果还是报错。。。这是一个解决方案,或许对一些人遇到的这个问题有用,但是我没有成功!

   之后有对配置文件展开了分析,考虑到了是不是persistence.xml的问题,它是放到src源文件夹下自己建的WEB-INF文件夹下面的,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
  <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:3308/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
      <property name="hibernate.hbm2dd1.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

这一次解决方案是对里面添加了 <provider>org.hibernate.ejb.HibernatePersistence</provider>这一行代码,指定了这个类,之后再运行测试类,运行后又出现了错误。。

java.lang.IncompatibleClassChangeError: class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class

现在又出现了这个错误。。,之后我又考虑了一下,到网上找了几个解决方案,最后总结了一下是jar包的问题,我是用的hibernate的包的版本是3.5.6的版本,有很多jar包都被里面hibernate3.0.jar集合了进去,比如:

hibernate3.5中的hibernate3.0包,已经包涵了hibernate-annotations,hibernate-commons-annotations.jar,commons-collections 这三个包,所以应该去掉commons-collections 包。但是我采取了另一种解决方案,移除hibernate-annotations,hibernate-commons-annotations.jar,成功。
移除了persistence.jar和ejb3-persistence.jar,添加了hibernate-jpa-2.0-api-1.0.0.Final.jar,最后运行测试类的时候,总算能够成功的运行了,出现了那个成功的结果,真的很令人兴奋。。。

  今天在这里把这点小经验和大家分享一下,希望大家能有点帮助。。

原文地址:https://www.cnblogs.com/shunxiyuan/p/2165500.html