无法使用.xml文件配置Hibernate EntityManager配置

发布于 2025-01-23 15:29:37 字数 2048 浏览 1 评论 0 原文

我尝试使用EntityManager编写一些代码,但是Hibernate已更新为Hibernate-Core(6.0.0.-final),并且使用New Hibernate 6.0我的旧代码不起作用

,我的代码不起作用: 我的pom.xml 在此处输入图像说明

我的persistence.xml文件

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
         http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">

<persistence-unit name="CRM">

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

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5678/postgres"/>
        <property name="hibernate.connection.username" value="postgres"/>
        <property name="hibernate.connection.password" value="postgres"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.show_sql" value="true"/>
    </properties>
</persistence-unit>

和我的主要方法

        EntityManagerFactory entityManagerFactory =
            Persistence.createEntityManagerFactory("CRM");

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();

    entityManager.persist(new SuperHero());

    entityManager.getTransaction().commit();

    entityManager.close();

    entityManagerFactory.close();

在这里结果

在此处输入图像说明

事先感谢您的帮助

I tried to write some code with EntityManager but hibernate was updated to hibernate-core(6.0.0.Final) and with new hibernate 6.0 my old codes doesn't work

There my code:
my pom.xml
enter image description here

my persistence.xml file

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
         http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">

<persistence-unit name="CRM">

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

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5678/postgres"/>
        <property name="hibernate.connection.username" value="postgres"/>
        <property name="hibernate.connection.password" value="postgres"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.show_sql" value="true"/>
    </properties>
</persistence-unit>

and my main method

        EntityManagerFactory entityManagerFactory =
            Persistence.createEntityManagerFactory("CRM");

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();

    entityManager.persist(new SuperHero());

    entityManager.getTransaction().commit();

    entityManager.close();

    entityManagerFactory.close();

here result

enter image description here

thanks in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腻橙味 2025-01-30 15:29:37

好像您正在混合2个不兼容版本的Hibernate Resources:::

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.0.0.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.6.7.Final</version>
</dependency>

V6开始,Hibernate库已从使用Java持久性转变为雅加达的持久性。您可以在其他地方阅读有关此较大的Java生态系统的更改 - 以及其他问题和答案。

通过包括Hibernate Entity Manager v5依赖关系,您的项目仍将参考Java持久库(例如,通过 Javax.persistence-api-2.2.2.2.2.2.jar 或类似)。这意味着您的代码可能仍会编译 - 但是,如您所见,它不会执行。您会看到涉及 Javax 类的错误消息,这些类不再由V6 Hibernate Core库支持。

此外,Hibernate的JPA支持已合并到Hibernate-Core模块中,使Hibernate-EntityManager模块过时了。您可以通过查看Entity Manager 5.6.7 JAR文件中的readme.txt文件来查看有关此的注释:

Hibernate的JPA支持已合并到Hibernate-Core模块中,使此Hibernate-EntityManager模块过时了。此模块将在Hibernate Orm 6.0中删除。

推荐的步骤:

  1. 从POM中删除 Hibernate-entityManager 依赖关系。这可能会触发一系列的编译错误,因为您将不再对诸如 javax.persistence.entitymanager 的类都有任何库支持。

  2. 将所有 Javax 导入到 jakarta 导入。因此,例如,从上面的(1)中读课,这变成了:

import jakarta.persistence.EntityManager;
  1. 在您的 persistence.xml 文件中,您还需要将任何类似的引用修复到 javax - for for示例:
<property name="jakarta.persistence.jdbc.driver" 
          value="com.mysql.jdbc.Driver" />

最终注释

如果您仍然遵循上述步骤遇到问题,则可以参考官方的Hibernate orm 6.0迁移指南

It looks as if you are mixing 2 incompatible versions of Hibernate resources:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.0.0.Final</version>
</dependency>

and:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.6.7.Final</version>
</dependency>

From v6 onwards, the Hibernate libraries have moved from using Java Persistence to Jakarta Persistence. You can read about this larger Java ecosystem change elsewhere Transition from Java EE to Jakarta EE - and also in other SO questions and answers.

By including a Hibernate Entity Manager v5 dependency, your project will still be referring to a Java Persistence library (e.g. via javax.persistence-api-2.2.jar or similar). This means your code may still compile - but, as you see, it will not execute. You will see error messages referring to javax classes, which are no longer supported by the v6 Hibernate Core library.

Furthermore, Hibernate's JPA support has been merged into the hibernate-core module, making the hibernate-entitymanager module obsolete. You can see a note about this by looking at the readme.txt file in your Entity Manager 5.6.7 JAR file:

Hibernate's JPA support has been merged into the hibernate-core module, making this hibernate-entitymanager module obsolete. This module will be removed in Hibernate ORM 6.0.

Recommended steps:

  1. Remove the hibernate-entitymanager dependency from your POM. That will probably trigger a series of compilation errors, because you will no longer have any library support for classes such as javax.persistence.EntityManager.

  2. Update all your javax imports to jakarta imports. So, for example, taking the class from (1) above, that becomes:

import jakarta.persistence.EntityManager;
  1. In your persistence.xml file you will also need to fix any similar references to javax - for example:
<property name="jakarta.persistence.jdbc.driver" 
          value="com.mysql.jdbc.Driver" />

Final Notes

If you still face issues following the above steps, then you can refer to the official Hibernate ORM 6.0 Migration Guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文