2010-11-23 9 views
5

私はJPAの初心者です。私はその本から簡単な例を試みました。 しかし、どんなに、私は次のエラーが表示されないものを:"EntityManagerの永続性プロバイダがありません"エラー

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named EmployeeService 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60) 
     at com.mycompany.simpleentity.EmployeeTest.main(EmployeeTest.java:18) 

私は多くのことをGoogleで検索し、私はJPAについて読んものをすべてやりました。ここ
は私のプロジェクトのディレクトリツリーである:ここで

. 
|-- pom.xml 
`-- src 
    |-- main 
    | |-- java 
    | | `-- com 
    | |  `-- mycompany 
    | |   `-- simpleentity 
    | |    |-- Employee.java 
    | |    |-- EmployeeService.java 
    | |    `-- EmployeeTest.java 
    | `-- resources 
    |  `-- META-INF 
    |   `-- persistence.xml 
    `-- test 

は私のpom.xmlである:ここでは

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mycompany</groupId> 
    <artifactId>SimpleEntity</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>SimpleEntity</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.persistence</groupId> 
     <artifactId>persistence-api</artifactId> 
     <version>1.0</version>  
    </dependency> 
    <dependency> 
     <groupId>postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.0-801.jdbc4</version> 
    </dependency> 
    </dependencies> 

    <build> 

     <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
      <source>1.5</source> 
      <target>1.5</target> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 

      <configuration> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <mainClass>com.mycompany.simpleentity.EmployeeTest</mainClass> 
         <!-- <classpathLayoutType>repository</classpathLayoutType> --> 
         <classpathMavenRepositoryLayout>true</classpathMavenRepositoryLayout> 
         <classpathPrefix>${env.HOME}/.m2/repository</classpathPrefix> 

        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 
     </plugins> 

    </build> 
</project> 

は、私のソースコードは次のとおりです。 EmployeeTest.java:

package com.mycompany.simpleentity; 

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

public class EmployeeTest { 
    public static void main(String[] args) { 
     EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService"); 
     EntityManager em = emf.createEntityManager(); 

    } 
} 

そしてここに私のpersistance.xmlがあります。

<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_1_0.xsd" 
     version="1.0"> 

    <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL"> 
     <class>com.mycompany.simpleentity.Employee</class> 
     <properties> 
      <property name="toplink.jdbc.driver" 
         value="org.postgresql.Driver"/> 
      <property name="toplink.jdbc.url" 
         value="jdbc:postgresql://localhost:5432/testdb;create=true"/> 
      <property name="toplink.jdbc.user" value="postgres"/> 
      <property name="toplink.jdbc.password" value="111"/> 

     </properties> 
    </persistence-unit> 
</persistence> 

どうすればいいですか? ありがとうございます。

答えて

7

JPAは、複数のJPAプロバイダ(Hibernate、EclipseLink、OpenJPA、Toplink)によって実装された仕様です。

使用するプロバイダーを選択し、pom.xmlに適切な依存関係を追加する必要があります。また、プロバイダをpersistence.xmlに指定する必要があります。例えば

あなたはOpenJPAのを使用する場合、(その最新バージョンは、Mavenのセントラル・レポで利用可能ですので、ベンダー固有のリポジ​​トリを設定する必要がないので、私はこの例のためにそれを選びました):

<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- Note that you don't need persistence-api dependency - it's transitive --> 
    <dependency> 
     <groupId>org.apache.openjpa</groupId> 
     <artifactId>openjpa-all</artifactId> 
     <version>2.0.1</version> 
    </dependency> 
    <dependency> 
     <groupId>postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.0-801.jdbc4</version> 
    </dependency> 
    </dependencies> 

<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_1_0.xsd"  
     version="1.0">  

    <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL"> 
     <!-- Provider specification --> 
     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> 

     <class>com.mycompany.simpleentity.Employee</class>  
     <properties>  
      <property name="javax.persistence.jdbc.driver"  
         value="org.postgresql.Driver"/>  
      <property name="javax.persistence.jdbc.url"  
         value="jdbc:postgresql://localhost:5432/testdb;create=true"/>  
      <property name="javax.persistence.jdbc.user" value="postgres"/>  
      <property name="javax.persistence.jdbc.password" value="111"/>  

     </properties>  
    </persistence-unit>  
</persistence> 
1

実際には

実際のPeristence Providerには依存していないようです。

JPA自体に実装がないため、実際の解決策としてHibernate/Toplink/OpenJPAも使用する必要があります。

0

persistence.xmlによると、PostgreSQLをRDBMSとしてTopLinkを使用しています。 pom.xmlでPostgreSQL JDBCドライバを参照している間は、TopLinkを依存関係として宣言していません。

私の推測では、永続性APIはクラスパス内でTopLink(永続性プロバイダ)を見つけることができません。依存関係としてTopLinkを追加してみてください。

2

このコード

<properties> 
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/Database name" /> 
      <property name="javax.persistence.jdbc.user" value="" /> 
      <property name="javax.persistence.jdbc.password" value="" /> 
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> 
</properties> 
2

1を使用するよりも、あなたはJPA +のEclipseLinkのプロバイダを使用している場合)あなたはどんなプロバイダの持続性プロバイダを()が定義されていますOpenJPAのために 例: <persistence-unit ...> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> ... ... </persistence-unit>

2)カスタムビルド/コンパイルプロセス(mavenなど)を使用している場合 Meta-INF/persistance.xmlがコンパイル済み/ classesフォルダにコピーされていることを確認してください。

+0

2番目の点を付けていただきありがとうございます。それが私の問題でした。ありがとう!!! – nashuald

0

JPAには複数のプロバイダ(実装者)があります。 JPA仕様の実際のバイトコードを提供するものを選択する必要があります。休止状態は一般的な選択ですが、あなたとは異なる場合があります。それを特定したら、それをあなたのPOMへの依存として追加してください。それ以外の場合は、ビルドパスにJARファイルを追加します。

関連する問題