如何更正此示例JPA Java应用程序?

时间:2014-08-07 16:55:33

标签: java mysql eclipse hibernate jpa

我尝试在 4处从此页面创建示例应用程序。点http://www.vogella.com/tutorials/JavaPersistenceAPI/article.html#installation

我的Hibernate示例应用程序

我已经使用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
  • 冬眠-公地注解-4.0.2.Final.jar
  • 冬眠核-4.2.3.Final.jar
  • 冬眠-JPA-2.0-API-1.0.1.Final.jar
  • 了Javassist-3.15.0-GA.jar
  • 的JBoss-测井3.1.0.GA.jar
  • 的JBoss-事务api_1.1_spec-1.0.1.Final.jar
  • MySQL的连接器的Java-5.1.29-bin.jar

我的JPA示例应用程序

我想使用JPA。我必须连接到我的MySQL数据库。我不确切知道如何解决这个问题。我也不知道应该将哪些.jar文件添加到我的项目中。

这是我的项目树:

enter image description here

这是添加的.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)

2 个答案:

答案 0 :(得分:0)

问题是您将持久性提供程序设置为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>

答案 1 :(得分:0)

您需要放置mysql jar并且还需要在persistence.xml中进行更正:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />

这对你肯定有帮助。