我想用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)
我错过了什么?我需要导入其他罐子吗?或者在描述符中设置其他内容?