2
votes

I tried to make an example application from this page at the 4. point: http://www.vogella.com/tutorials/JavaPersistenceAPI/article.html#installation

My sample application with Hibernate

I made already a sample application with Hibernate. It was working fine. This is the content of the hibernate config file:

<?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>

To this sample application with Hibernate I added these .jar files to my project:

  • 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

My sample application with JPA

I would like to use JPA. I have to connect to my MySQL Database. I don't know exactly how can I solve this. I also don't know exactly which .jar files should I add to my project.

This is my project tree:

enter image description here

This is the added .jar files:

  • eclipselink.jar
  • javax.persistence_2.1.0.v201304241213.jar

This is the persistence.xml file:

<?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> 

I modified this persistence.xml file (because the answer of Jakub Kubrynski and 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> 

(I get the same error)

This is a Todo.java file which I should write to the database:

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
        + "]";
  }

} 

This is the Main.java file, where the program start:

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();
  }
} 

I get this error message:

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
Unfortunately I don't know the exact solution to the problem, but the following tutorial may be helpful to integrate the Hibernate solution with JPA: uaihebert.com/tutorial-hibernate-3-with-jpa-2 - EpicPandaForce
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" /> this line is wrong in your app, you are trying to use mySQL database and not derby, so you need a mysql driver for it. Change it to <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />. - EpicPandaForce
Also change <property name="javax.persistence.jdbc.url" value="jdbc:derby:/home/vogella/databases/simpleDb;create=true" /> to <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306</property> - EpicPandaForce
I modified the xml file and get the same error: 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) - Kroy
Okay, try changing <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> to <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - EpicPandaForce

2 Answers

0
votes

The problem is that you set persistence provider to be org.apache.openjpa.persistence.PersistenceProviderImpl and you're trying to use Hibernate. To do that please use 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>

0
votes

You need to put the mysql jar and and also need to make corrections in persistence.xml:

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

this will help you surely.