作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试开始了解 Hibernate 。但我有一个问题。通过我的研究,我发现了很多东西,但没有什么可以解决我的问题。“org.hibernate.ejb.HibernatePersistence 以红色书写。
我的 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_2_0.xsd"
version="2.0">
<persistence-unit name="demojpa">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.format_sql" value="true"/>
<!-- Configuration de la BDD -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/demojpa" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<!-- Specifie le dialecte SQL utilisé pour communiquer avec la BDD -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MYSQLDialect"/>
<!-- Indique a Hibernate de re-creer la BDD au lancement de l'applciation -->
<property name="hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
我的类(class)
public class DemoJpa {
public static void main(String[] args) {
// 1 Ouverture unité de travail JPA
EntityManagerFactory emf = Persistence.createEntityManagerFactory("demojpa");
EntityManager em = emf.createEntityManager();
}
要完成我的根的照片...因为我觉得这个有问题......
感谢大家的帮助!
最佳答案
在您的 persistence.xml
中替换 deprecated 声明(在 Hibernate 版本 5+ 中删除)
<provider>org.hibernate.ejb.HibernatePersistence</provider>
与
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
基本上,这应该会使“红色”消失。
请注意,4.3.x 版本中的 Hibernate supports JPA 2.1 (请参阅表支持的 JPA 版本)。重要提示:您还应该在 persistence.xml 的头部声明以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<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">
这将为您提供完整的 JPA 2.1 支持并告诉 ORM 使用 2.1 版本。
由于您刚刚开始使用,最好从版本 5.2 的 Hibernate 开始或更高版本,因为这些版本的支持时间将超过 4.3.x,4.3.x 已被视为“仅维护”,不再积极改进或开发。
仅供引用,persistence provider deprecation topic前面已经讨论过。
希望有帮助。
关于java - 错误: No persistence provider for EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49233655/
我是一名优秀的程序员,十分优秀!