gpt4 book ai didi

jpa-2.0 - 没有可用的持久性提供程序

转载 作者:行者123 更新时间:2023-12-02 13:46:14 25 4
gpt4 key购买 nike

我的文件src/main/resources/META-INF/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="todo" transaction-type="RESOURCE_LOCAL">
<class>com.testJPA.classes.Voiture</class>
<properties>

<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:todos" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
</properties>

</persistence-unit>

我的主要类(class)是:

package com.testJPA.classes;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.print("Hello");
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("todo");
EntityManager em = emf.createEntityManager();

for (int i = 0; i < 10; i++) {
Voiture car = new Voiture("a", "b", "c");
em.persist(car);

}

em.getTransaction().commit();

// Find the number of Point objects in the database:
Query q1 = em.createQuery("SELECT COUNT(v) FROM Voiture v");
System.out.println("Le nombre d'enregistrement: "
+ q1.getSingleResult());

}

}

我的类(class)“Voiture”是:

package com.testJPA.classes;

import javax.persistence.Entity;
import javax.persistence.Id;



@Entity
public class Voiture {


@Id private String matricule;
String position;
String vitesse;

public Voiture(String matricule, String position, String vitesse) {
this.matricule = matricule;
this.position = position;
this.vitesse = vitesse;


}

public Voiture() {
this.matricule = "";
this.position = "";
this.vitesse = "";

}

public String getMatricule() {
return matricule;
}

public void setMatricule(String matricule) {
this.matricule = matricule;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}

public String getVitesse() {
return vitesse;
}

public void setVitesse(String vitesse) {
this.vitesse = vitesse;
}

}

我遇到此错误:尝试以下发现的实现后,没有可用于“todo”的持久性提供程序

抱歉,我是 JAva EE 初学者

最佳答案

这是一个类路径问题。您需要在已编译类目录的根目录中有 META-INF 目录。

target / classes /
/ com / testJPA / classes / Main.class
/ META-INF / persistence.xml

解决此问题后,您需要删除 openjpa.RuntimeUnenhancedClasses 属性。这是一种邪恶的属性,会让你头疼不已。请阅读this documentationthis documentation关于如何正确增强您的实体。

关于jpa-2.0 - 没有可用的持久性提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10818025/

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