无法使用.xml文件配置Hibernate EntityManager配置
我尝试使用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();
在这里结果
事先感谢您的帮助
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好像您正在混合2个不兼容版本的Hibernate Resources:::
从
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文件来查看有关此的注释:
推荐的步骤:
从POM中删除
Hibernate-entityManager
依赖关系。这可能会触发一系列的编译错误,因为您将不再对诸如javax.persistence.entitymanager
的类都有任何库支持。。
将所有
Javax
导入到jakarta
导入。因此,例如,从上面的(1)中读课,这变成了:persistence.xml
文件中,您还需要将任何类似的引用修复到javax
- for for示例:最终注释
如果您仍然遵循上述步骤遇到问题,则可以参考官方的Hibernate orm 6.0迁移指南。
It looks as if you are mixing 2 incompatible versions of Hibernate resources:
and:
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 tojavax
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:
Recommended steps:
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 asjavax.persistence.EntityManager
.Update all your
javax
imports tojakarta
imports. So, for example, taking the class from (1) above, that becomes:persistence.xml
file you will also need to fix any similar references tojavax
- for example:Final Notes
If you still face issues following the above steps, then you can refer to the official Hibernate ORM 6.0 Migration Guide.