gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:34:06 25 4
gpt4 key购买 nike

使用 Play ! Framework 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]

最佳答案

我相信您需要将一个 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>您也将使用。

关于JPA 和 Play Framework : No Persistence provider for EntityManager named update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11313466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com