gpt4 book ai didi

java - hibernate 错误: No Persistence provider for EntityManager named my-unit

转载 作者:行者123 更新时间:2023-12-01 17:58:53 35 4
gpt4 key购买 nike

我尝试将我的 persistence.xml 放入 Java 项目的各个部分,并使用以下命令调用它:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("my-unit");

但它返回一个错误:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named my-unit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at main.MainClass.main(MainClass.java:81)
Process finished with exit code 1

我的persistence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
<persistence-unit name="my-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- use encache provider-->
<!--<property name="hibernate.cache.region.factory_class"-->
<!--value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>-->
<!--<property name="hibernate.javax.cache.provider" value="org.ehcache.jsr107.EhcacheCachingProvider"/>-->
<!--&lt;!&ndash;use second level cache&ndash;&gt;-->
<!--<property name="hibernate.cache.use_second_level_cache" value="true"/>-->
<!--&lt;!&ndash;use 3rd level cache&ndash;&gt;-->
<!--<property name="hibernate.cache.use_query_cache" value="true"/>-->
</properties>
</persistence-unit>

我使用 IntelliJ 作为 Java IDE,并且 persistence.xml 文件位于 resources 文件夹中的 META-INF 文件夹下,我已经尝试过检查项目结构,一切看起来都不错。我使用 hibernate 作为 Maven 存储库。感谢您的帮助

最佳答案

您需要定义持久性提供程序名称

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

这样就可以解决问题了。

关于java - hibernate 错误: No Persistence provider for EntityManager named my-unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60669341/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com