3

我的文件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 的初学者

4

2 回答 2

3

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

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

解决此问题后,您需要摆脱 openjpa.RuntimeUnenhancedClasses 属性。那是一个邪恶的属性,会让你头疼不已。请阅读本文档本文档以了解如何正确增强您的实体。

于 2012-05-30T22:33:18.417 回答
0

如果 JPA 是独立使用的,你必须根据你使用<provider>persistence.xmlJPA 实现来指定

对于 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>
于 2012-05-30T14:26:09.047 回答