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

时间:2012-05-30 14:11:17

标签: jpa-2.0 provider openjpa

我的档案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>

我的主要课程是:

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

    }

}

和我的歌曲“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的初学者

2 个答案:

答案 0 :(得分:2)

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

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

解决此问题后,您需要删除openjpa.RuntimeUnenhancedClasses属性。这是一个邪恶的财产,将导致你没有头痛的结束。请阅读this documentationthis documentation,了解如何正确增强您的实体。

答案 1 :(得分:0)

如果JPA是独立使用的,则必须根据您使用的JPA实现在<provider>中指定persistence.xml

对于OpenJPA,它是org.apache.openjpa.persistence.PersistenceProviderImpl

    <persistence-unit name="todo" transaction-type="RESOURCE_LOCAL">
        <class>com.testJPA.classes.Voiture</class>
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl/provider>   
         .......................................................
    </persistence-unit>