使用 Hibernate 作为提供程序的 JPA 2.0 - 异常:EntityManager 没有持久性提供程序

发布于 2024-12-10 10:47:02 字数 5737 浏览 0 评论 0原文

我正在尝试按照 Hibernate EntityManager 文档。我已经研究了几个小时了,但无论我做什么,当我尝试创建 EntityManagerFactory 时,我总是会遇到此异常:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at se.mycomp.UserTest.main(UserTest.java:9)

我发现了很多关于此异常的类似问题,但没有我能够解决的解决方案开始工作吧。我在这里做错了什么?

目录结构

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── se
    │   │       └── mycomp
    │   │           ├── UserTest.java
    │   │           └── domain
    │   │               └── User.java
    │   └── resources
    │       ├── META-INF
    │       │   └── persistence.xml
    │       └── log4j.properties
    └── test
        └── java

我的persistence.xml

<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="manager1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>se.mycomp.domain.User</class> 
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
            <property name="javax.persistence.jdbc.user" value="test"/>
            <property name="javax.persistence.jdbc.password" value="1234"/>
        </properties>
    </persistence-unit>
</persistence>

我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>se.lil.tryjpa</groupId>
<artifactId>try-jpa</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <hibernate-core.version>3.6.4.Final</hibernate-core.version>
    <mysql-connector-java.version>5.1.16</mysql-connector-java.version>
    <slf4j.version>1.6.1</slf4j.version>
    <log4j.version>1.6.1</log4j.version>
</properties>

<dependencies>
    <!-- HIBERNATE DEPENDENCIES -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-core.version}</version>
    </dependency>

    <!-- MYSQL DEPENDENCIES -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-java.version}</version>
    </dependency>

    <!-- Logging Dependencies -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${log4j.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <optimize>true</optimize>
                <debug>true</debug>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <downloadSources>true</downloadSources>
            </configuration>
        </plugin>
    </plugins>
</build>

UserTest.java

public class UserTest {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
        EntityManager em = emf.createEntityManager();
    }
}

I'm trying to set up a simple jpa 2.0 project by following the information in the Hibernate EntityManager documentation. I've been on this for hours now, but no matter what I do I always get this exception when I try to create a EntityManagerFactory:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at se.mycomp.UserTest.main(UserTest.java:9)

I've found quite a few similar questions regarding this exception, but no solutions that I am able to get to work. What am I doing wrong here?

directory structure

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── se
    │   │       └── mycomp
    │   │           ├── UserTest.java
    │   │           └── domain
    │   │               └── User.java
    │   └── resources
    │       ├── META-INF
    │       │   └── persistence.xml
    │       └── log4j.properties
    └── test
        └── java

my persistence.xml

<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="manager1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>se.mycomp.domain.User</class> 
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
            <property name="javax.persistence.jdbc.user" value="test"/>
            <property name="javax.persistence.jdbc.password" value="1234"/>
        </properties>
    </persistence-unit>
</persistence>

my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>se.lil.tryjpa</groupId>
<artifactId>try-jpa</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <hibernate-core.version>3.6.4.Final</hibernate-core.version>
    <mysql-connector-java.version>5.1.16</mysql-connector-java.version>
    <slf4j.version>1.6.1</slf4j.version>
    <log4j.version>1.6.1</log4j.version>
</properties>

<dependencies>
    <!-- HIBERNATE DEPENDENCIES -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-core.version}</version>
    </dependency>

    <!-- MYSQL DEPENDENCIES -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-java.version}</version>
    </dependency>

    <!-- Logging Dependencies -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${log4j.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <optimize>true</optimize>
                <debug>true</debug>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <downloadSources>true</downloadSources>
            </configuration>
        </plugin>
    </plugins>
</build>

UserTest.java

public class UserTest {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
        EntityManager em = emf.createEntityManager();
    }
}

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

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

发布评论

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

评论(3

我早已燃尽 2024-12-17 10:47:02

也许您错过了 pom.xml 依赖项中的 Provider 类或其依赖项之一?

您提供给 hibernate 文档的链接表示您还应该添加

<project ...>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate-core-version}</version>
    </dependency>
  </dependencies>
</project>

到 pom.xml

Maybe you miss the Provider class or one of its dependencies in your pom.xml dependencies?

The link you give to the hibernate docs says that you should also add

<project ...>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate-core-version}</version>
    </dependency>
  </dependencies>
</project>

to your pom.xml

鹿港小镇 2024-12-17 10:47:02

persistence.xml 应该存在于 META-INF 目录中,而 META-INF 应该存在于应用程序的类路径(即 src 文件夹)中。

根据您的文件夹结构存在于资源文件夹中,尝试将其移动到它应该工作的类路径。

persistence.xml is meant to be present in META-INF directory and META-INF is meant to be present in the classpath of the application which is src folder.

As per your folder structure its present in resource folder, try moving it to classpath it sholud work.

稍尽春風 2024-12-17 10:47:02

我使用下面的依赖项和以下提供程序解决了这个问题:

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


 <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1200-jdbc41</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>LATEST</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>LATEST</version>
</dependency>

最新版本不适用于您,您可以使用
hibernate-jpa-2.0-api 的 1.0.1.Final
和 hibernate-entitymanager 的 5.2.5.Final

另外,在 persistence.xml 中,不要忘记 hbm2dll.auto 属性(这是我花了一些时间弄清楚的一个问题)

<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>

然后进行 Maven 全新安装

I got this problem solved using the dependencies below and the following provider:

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


 <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1200-jdbc41</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>LATEST</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>LATEST</version>
</dependency>

is LATEST doesn't work as Version for you, you can use
1.0.1.Final for hibernate-jpa-2.0-api
and 5.2.5.Final for hibernate-entitymanager

Also, in persistence.xml, don't forget hbm2dll.auto properties (this was one issue I've spent some time figuring out)

<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>

Then make a maven clean install

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