如何更正此示例 JPA Java 应用程序?
Posted
技术标签:
【中文标题】如何更正此示例 JPA Java 应用程序?【英文标题】:How can I correct this Example JPA Java application? 【发布时间】:2014-10-01 00:35:59 【问题描述】:我试图在 4.要点: http://www.vogella.com/tutorials/JavaPersistenceAPI/article.html#installation
我的 Hibernate 示例应用程序
我已经使用 Hibernate 制作了一个示例应用程序。它工作正常。这是休眠配置文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">mintaalkalmazas2</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Kerdes.hbm.xml"/>
<mapping resource="TestTable.hbm.xml"/>
</session-factory>
</hibernate-configuration>
在这个带有 Hibernate 的示例应用程序中,我将这些 .jar 文件添加到我的项目中:
antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.2.Final.jar hibernate-core-4.2.3.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar javassist-3.15.0-GA.jar jboss-logging-3.1.0.GA.jar jboss-transaction-api_1.1_spec-1.0.1.Final.jar mysql-connector-java-5.1.29-bin.jar我的 JPA 示例应用程序
我想使用 JPA。我必须连接到我的 MySQL 数据库。我不知道我该如何解决这个问题。我也不知道应该将哪些 .jar 文件添加到我的项目中。
这是我的项目树:
这是添加的 .jar 文件:
eclipselink.jar javax.persistence_2.1.0.v201304241213.jar这是 persistence.xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>Todo</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:/home/vogella/databases/simpleDb;create=true" />
<property name="javax.persistence.jdbc.user" value="test" />
<property name="javax.persistence.jdbc.password" value="test" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
我修改了这个 persistence.xml 文件(因为 Jakub Kubrynski 和 Zhuinden 的回答):
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>Todo</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306"></property>
<property name="javax.persistence.jdbc.user" value="test" />
<property name="javax.persistence.jdbc.password" value="test" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
(我得到同样的错误)
这是一个 Todo.java 文件,我应该将它写入数据库:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Todo
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String summary;
private String description;
public String getSummary()
return summary;
public void setSummary(String summary)
this.summary = summary;
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
@Override
public String toString()
return "Todo [summary=" + summary + ", description=" + description
+ "]";
这是 Main.java 文件,程序从这里开始:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main
private static final String PERSISTENCE_UNIT_NAME = "todos";
private static EntityManagerFactory factory;
public static void main(String[] args)
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// read the existing entries and write to console
Query q = em.createQuery("select t from Todo t");
List<Todo> todoList = q.getResultList();
for (Todo todo : todoList)
System.out.println(todo);
System.out.println("Size: " + todoList.size());
// create new todo
em.getTransaction().begin();
Todo todo = new Todo();
todo.setSummary("This is a test");
todo.setDescription("This is a test");
em.persist(todo);
em.getTransaction().commit();
em.close();
我收到此错误消息:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named todos
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at Main.main(Main.java:15)
【问题讨论】:
很遗憾我不知道该问题的确切解决方案,但以下教程可能有助于将 Hibernate 解决方案与 JPA 集成:uaihebert.com/tutorial-hibernate-3-with-jpa-2<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
这行在您的应用程序中是错误的,您正在尝试使用 mySQL 数据库而不是 derby,因此您需要一个 mysql 驱动程序。将其更改为<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
。
也将<property name="javax.persistence.jdbc.url" value="jdbc:derby:/home/vogella/databases/simpleDb;create=true" />
改为<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306</property>
我修改了 xml 文件并得到了相同的错误:线程“main”中的异常 javax.persistence.PersistenceException:在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java 中没有名为 todos 的 EntityManager 的持久性提供程序:85) 在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) 在 Main.main(Main.java:15)
好的,试试把<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
改成<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
【参考方案1】:
需要把mysql的jar放到persistence.xml
中,还需要更正:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
这肯定会对你有所帮助。
【讨论】:
【参考方案2】:问题是您将持久性提供程序设置为 org.apache.openjpa.persistence.PersistenceProviderImpl
并且您正在尝试使用 Hibernate。为此,请使用org.hibernate.jpa.HibernatePersistenceProvider
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>Todo</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:/home/vogella/databases/simpleDb;create=true" />
<property name="javax.persistence.jdbc.user" value="test" />
<property name="javax.persistence.jdbc.password" value="test" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
【讨论】:
我做到了,但我得到了相同的错误消息:线程“main”中的异常 javax.persistence.PersistenceException:在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java 中没有名为 todos 的 EntityManager 的持久性提供程序:85) 在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) 在 Main.main(Main.java:15) 你能把这个项目上传到github吗?目前看起来你不知道你在做什么。为什么要包含mysql驱动程序。?德比依赖等在哪里 我将项目上传到我的保管箱公用文件夹:dl.dropboxusercontent.com/u/47788456/***/JPAJava.zip 他正在尝试使用教程的内容将其应用于他的 mySql 数据库,而不是 derby 数据库。以上是关于如何更正此示例 JPA Java 应用程序?的主要内容,如果未能解决你的问题,请参考以下文章
如何更正此错误:java.lang.OutOfMemoryError
示例 Struts2 spring4.1 jpa/hibernate3.1.6 中的 java.lang.reflect.InvocationTargetException