2012-05-30 66 views
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; 
    } 

} 

我有這個錯誤:沒有嘗試以下發現實現

對不起,我是begineer在Java EE後用於「待辦事項」持久性提供

+0

如果發佈整個堆棧跟蹤可能會有所幫助。 – Rick

回答

0

如果JPA使用獨立的,你必須根據你使用的JPA實現在persistence.xml指定<provider>

對於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> 
+0

我添加它,但總是相同的錯誤,對不起 – user1528481

2

這是一個classpath的問題。您需要在編譯的classes目錄的根目錄下有META-INF目錄。

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

修復此問題後,您需要刪除openjpa.RuntimeUnenhancedClasses屬性。那是一種邪惡的財產,會讓你頭痛不已。請閱讀this documentationthis documentation關於如何正確增強您的實體。