2

我想用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)

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

4

1 回答 1

5

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

如果您在hibernate 主页上查看组件如何绑定在一起的图片,您会看到实体管理器构建在 hibernate 核心和 hibernate 注释之上。您需要为这两个及其所有依赖项添加所需的 jar。

文档描述了 hibernate 发行版中的各种 jar 文件夹,以及您需要哪些 jar 集以用于何种用途。

于 2012-09-15T01:35:35.180 回答