遇到过时的 javax.persistence.spi.PersistenceProvider

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21693139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:06:01  来源:igfitidea点击:

Encountered a deprecated javax.persistence.spi.PersistenceProvider

javaspringhibernatejpa

提问by eaststrong

when you use spring & Hibernate, have you ever met a log warning that says

当您使用 spring & Hibernate 时,您是否遇到过日志警告说

WARN o.hibernate.ejb.HibernatePersistence - HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.

警告 o.hibernate.ejb.HibernatePersistence - HHH015016:遇到不推荐使用的 javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence];使用 [org.hibernate.jpa.HibernatePersistenceProvider] 代替。

How to handle that? Thank you for any answer.

怎么处理?谢谢你的任何回答。

回答by Andre Hofmeister

It should be

它应该是

org.hibernate.jpa.HibernatePersistenceProvider

Have a look at this.

看看这个

Deprecated.

Use HibernatePersistenceProviderinstead

已弃用。

使用HibernatePersistenceProvider代替

回答by isma.imc

If you are working with Spring Data JPA and Java Configuration, you will be able to solve it, adding the following code in your Entity Manager Factory:

如果您正在使用 Spring Data JPA 和 Java Configuration,您将能够解决它,在您的实体管理器工厂中添加以下代码:

factory.setPersistenceProvider(new HibernatePersistenceProvider());

factory.setPersistenceProvider(new HibernatePersistenceProvider());

@Bean
    public EntityManagerFactory entityManagerFactory() throws SQLException {

      HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      vendorAdapter.setGenerateDdl(true);
      vendorAdapter.setShowSql(true);

      LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
      factory.setJpaVendorAdapter(vendorAdapter);
      **factory.setPersistenceProvider(new HibernatePersistenceProvider());**
      factory.setPackagesToScan("com.company.appname.persistence.domain");
      factory.setDataSource(dataSource());

      factory.setJpaProperties(hibernateProperties());
      factory.afterPropertiesSet();

      return factory.getObject();
    }

You will find a good example of Hibernate configuration with Spring Data JPA here: http://spring.io/guides/tutorials/data/3/

您将在此处找到使用 Spring Data JPA 进行 Hibernate 配置的一个很好的示例:http: //spring.io/guides/tutorials/data/3/

回答by cripox

Had this problem while working with JPA's Entity Manager in Spring context, having transaction-type="RESOURCE_LOCAL" in persistence.xml.

在 Spring 上下文中使用 JPA 的实体管理器时遇到了这个问题,在 persistence.xml 中有事务类型 =“RESOURCE_LOCAL”。

It's not always a bug. I actually had the wrong provider configured.

这并不总是一个错误。我实际上配置了错误的提供程序。

I just changed the provider in persistence.xml from

我刚刚改变了pe​​rsistence.xml中的提供者

<provider>org.hibernate.ejb.HibernatePersistence</provider>

to

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

and it works fine.

它工作正常。

Notice that the package changed from EJBto JPA

注意包从EJB变成了JPA

回答by Lucky

You get this message because the class org.hibernate.ejb.HibernatePersistenceis deprecated. Under my persistence.xmlfile I found the provider class had org.hibernate.ejb.HibernatePersistenceand I changed it to org.hibernate.jpa.HibernatePersistenceProvideras mentioned in the stacktrace warning message.

您收到此消息是因为该类org.hibernate.ejb.HibernatePersistence已被弃用。在我的persistence.xml文件下,我找到了提供程序类org.hibernate.ejb.HibernatePersistence,并将其更改org.hibernate.jpa.HibernatePersistenceProvider为堆栈跟踪警告消息中提到的。

persistence.xml

持久化文件

<persistence-unit name="personPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>Person</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db_name"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        ....
    </properties>
</persistence-unit>

回答by Hubert Kauker

For users who are not using SPRING:

对于不使用 SPRING 的用户:

We can replace the standard javax.persistencebootstrapping by a Hibernate specific one.

我们可以用Hibernate 特定的引导程序替换标准的javax.persistence引导程序。

Old:

老的:

EntityManagerFactory emf = Persistence.createEntityManagerFactory(
    PERSISTENCE_UNIT, props );

New:

新的:

PersistenceProvider provider = new HibernatePersistenceProvider();
EntityManagerFactory emf = provider.createEntityManagerFactory(
   PERSISTENCE_UNIT, props);

The deprecatedwarnings should now be gone. The problem was still present in 4.3.1.Final. In 5.1.0.Finalit should be fixed.

弃用警告现在应该走了。问题仍然存在于4.3.1.Final 中。在5.1.0.Final它应该被修复。

回答by gralVilla

I changed the reference to:

我将参考更改为:

org.hibernate.jpa.HibernatePersistenceProvider

but it didn't work.

但它没有用。

Then I removed all the references to Hibernate 4.x jar libs, downloaded last version (5.2.7), then added this jar files and it finally works.

然后我删除了对 Hibernate 4.x jar 库的所有引用,下载了最新版本(5.2.7),然后添加了这个 jar 文件,它终于可以工作了。

回答by user8191252

After changing your org.hibernate.ejb.HibernatePersistenceto org.hibernate.jpa.HibernatePersistenceProviderin the persistence.xmlChange also the hibernate-entitymanagerdependency version, get the last version 5.2.10.Final that fixed the bug. Here is:

更改后org.hibernate.ejb.HibernatePersistence,以org.hibernate.jpa.HibernatePersistenceProviderpersistence.xml变化也是hibernate-entitymanager依赖的版本,得到最后版本5.2.10.Final是固定的bug。这是:

http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager/5.2.10.Final

http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager/5.2.10.Final

it's worked for me

它对我有用