Jes Chergui
Jes Chergui

Reputation: 1318

JPA and Play Framework: No Persistence provider for EntityManager named update

Using Play! Framework 2.0.2, when I add several items from my java project to my H2 test database I only see one single item in the ITEM table. The single item being the last entry that i've persisted. I thought that this my be due the db being recreated at every commit. I therefor thought of adding the JPA.ddl=update property in my application.conf file. But this simply breaks with the following error. What

Here is my code (in the Item.save() method):

package models;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Persistence;

@Entity
public class Item {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int id;
    public String name;
    public String dev;
    public String type;
    public int quantity;
    public BigDecimal unitPrice;

    public Item() {}

    public Item(String name, String dev, String type, int quantity,
            BigDecimal unitPrice) {
        super();
        this.name = name;
        this.dev = dev;
        this.type = type;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
    }

    /**
     * Insert this new computer.
     */
    public void save() {
        //this.id = id;
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("defaultPersistenceUnit");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(this);        
        entityManager.getTransaction().commit();
        entityManager.close();
    }
}

Here is the error message

Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named update
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.
Final]
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.
Final]
        at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play_2.9.1.jar:2.0.2]
        at play.api.Play$$anonfun$start$1.apply(Play.scala:60) ~[play_2.9.1.jar:2.0.2]
        at play.api.Play$$anonfun$start$1.apply(Play.scala:60) ~[play_2.9.1.jar:2.0.2]
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) ~[scala-library.jar:0.11.3]

Upvotes: 3

Views: 5617

Answers (1)

Jeff LaJoie
Jeff LaJoie

Reputation: 1725

I believe you're going to need a persistence.xml file to be contained within your /conf/META-INF/ directory, and from there need to define a persistence unit. I believe this is because you're using Hibernate correct?

An example of what yours can look like

<persistence 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"
             version="2.0">

    <persistence-unit name="update">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.connection.url" value="jdbc:h2:mem:events"/>
        </properties>
    </persistence-unit>

</persistence>

In your tag you'll also need to include any <jar-file> or <class> you are to be using as well.

Upvotes: 4

Related Questions