3

使用播放!框架 2.0.2,当我将 java 项目中的几个项目添加到我的 H2 测试数据库时,我在 ITEM 表中只看到一个项目。单个项目是我坚持的最后一个条目。我认为这是由于每次提交时都会重新创建数据库。因此,我想在我的 application.conf 文件中添加 JPA.ddl=update 属性。但这只是因以下错误而中断。什么

这是我的代码(在 Item.save() 方法中):

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

这是错误消息

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]
4

1 回答 1

4

我相信您需要将 persistence.xml 文件包含在您的 /conf/META-INF/ 目录中,并从那里定义一个持久性单元。我相信这是因为您使用的是 Hibernate 对吗?

您的外观示例

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

在您的标签中,您还需要包含任何<jar-file><class>您将要使用的标签。

于 2012-07-03T20:06:33.827 回答