gpt4 book ai didi

hibernate - 无法使用 JPA EntityManager

转载 作者:行者123 更新时间:2023-12-02 22:30:00 26 4
gpt4 key购买 nike

我想用 Java 实现一个能够与数据库交互的程序。我已经使用 EJB 做了类似的事情,但这次我需要它能够在没有应用程序服务器的情况下工作。到目前为止我所做的是(使用 Eclipse):

  • 创建数据库连接
  • 创建一个 JPA 项目
  • 创建实体(正确映射)
  • 设置persistence.xml(在META-INF文件夹中)
  • 导入 MySQL JDBC Driver 库和 persistence-api-1.0.2.jar

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="top" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>database.Match</class>
<class>database.Player</class>
<class>database.Tournament</class>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/top"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

下面是我尝试使用 EntityManager 的方法:

private static EntityManagerFactory factory;
private EntityManager entityManager;

public DBManager() {
factory = Persistence.createEntityManagerFactory("top");
entityManager = factory.createEntityManager();
}

public Player createPlayer(String name, int age, Court favouriteCourt, int currentRanking) {
Player player = new Player();
player.setAge(age);
player.setCurrentRanking(currentRanking);
player.setFavouriteCourt(favouriteCourt);
player.setName(name);
entityManager.persist(player);
return player;
}

我的 Main 类只是简单地创建了一个新的 DBManager 并尝试使用 createPlayer

我得到以下异常:

    Exception in thread "main" javax.persistence.PersistenceException: No resource files named META-INF/services/javax.persistence.spi.PersistenceProvider were found. Please make sure that the persistence provider jar file is in your classpath.
at javax.persistence.Persistence.findAllProviders(Persistence.java:167)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at database.DBManager.<init>(DBManager.java:17)
at database.Main.main(Main.java:8)

添加 hibernate-entitymanager-3.3.2.GA.jar 我得到:

    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named top: Provider named org.hibernate.ejb.HibernatePersistence threw unexpected exception at create EntityManagerFactory: 
java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: org/hibernate/MappingException
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:124)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at database.DBManager.<init>(DBManager.java:17)
at database.Main.main(Main.java:8)
Caused by: java.lang.ClassNotFoundException: org.hibernate.MappingException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more



at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at database.DBManager.<init>(DBManager.java:17)
at database.Main.main(Main.java:8)

我错过了什么?我需要导入其他 jar 吗?或者在描述符中设置其他内容?

最佳答案

您缺少的是 hibernate-entitymanager 仅包含包装 hibernate 核心以将其公开为 JPA 实体管理器的包装器。因此,如果您只添加了您将不会获得实际的 hibernate 类,因此找不到您的类。

如果您查看组件如何在 hibernate home page 上连接在一起的图片您会看到实体管理器建立在 hibernate 核心和 hibernate 注释之上。您需要为这两者及其所有依赖项添加所需的 jar。

The docs描述 hibernate 发行版中的各种 jar 文件夹,以及具体使用哪种 jar 集。

关于hibernate - 无法使用 JPA EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12433805/

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