2017-07-20 23 views
1

私はJPAで単純なスプリングWebアプリケーションを設定しようとしていますが、mvn spring-boot:runを実行した後、次のスタックトレースを取得しました。もちろん、私は検索しようとしましたが、クラスが "クラスパス"にないことを発見しましたが、クラスパスがこのコンテキストで何を意味するのか、どうすればそれに何かを追加できますか?だから少しの助けが素晴らしいだろう。クラスが見つかりません:javax.persistence.spi.ProviderUtil

@SpringBootApplication 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 

     Partner partner = new Partner("test", "test", "test", "test", "test", "test"); 

     EntityManager em = PersistenceManager.INSTANCE.getEntityManager(); 
     em.getTransaction().begin(); 
     em.persist(partner); 
     em.getTransaction().commit(); 
     em.close(); 
     PersistenceManager.INSTANCE.close(); 

    } 

} 

永続マネージャの列挙:

public enum PersistenceManager { 
    INSTANCE; 

    private EntityManagerFactory emFactory; 

    private PersistenceManager() { 
     emFactory = Persistence.createEntityManagerFactory("sga-jpa"); 
    } 

    public EntityManager getEntityManager() { 
     return emFactory.createEntityManager(); 
    } 

    public void close() { 
     emFactory.close(); 
    } 

} 

私のpersistence.xml:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" 
      version="2.1"> 
    <persistence-unit name="sga-jpa" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
     <properties> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/sg_be" /> 
      <property name="javax.persistence.jdbc.user" value="root" /> 
      <property name="javax.persistence.jdbc.password" value="" /> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" /> 
      <property name="hibernate.show_sql" value="true" /> 
      <property name="hibernate.format_sql" value="true" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
      <property name="hibernate.hbm2ddl.auto" value="validate" /> 

      <property name="hibernate.c3p0.min_size" value="5" /> 
      <property name="hibernate.c3p0.max_size" value="20" /> 
      <property name="hibernate.c3p0.timeout" value="500" /> 
      <property name="hibernate.c3p0.max_statements" value="50" /> 
      <property name="hibernate.c3p0.idle_test_period" value="2000" /> 
     </properties> 
    </persistence-unit> 
</persistence> 
ここ

[WARNING] 
java.lang.reflect.InvocationTargetException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:498) 
     at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:506) 
     at java.lang.Thread.run(Thread.java:748) 
Caused by: java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil 
     at java.lang.Class.getDeclaredConstructors0(Native Method) 
     at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) 
     at java.lang.Class.getConstructor0(Class.java:3075) 
     at java.lang.Class.newInstance(Class.java:412) 
     at javax.persistence.Persistence.findAllProviders(Persistence.java:112) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60) 
     at hu.creativetribes.sga.configurations.PersistenceManager.<init>(PersistenceManager.java:13) 
     at hu.creativetribes.sga.configurations.PersistenceManager.<clinit>(PersistenceManager.java:8) 
     at hu.creativetribes.sga.Application.main(Application.java:18) 
     ... 6 more 
Caused by: java.lang.ClassNotFoundException: javax.persistence.spi.ProviderUtil 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
     ... 16 more 

は、オブジェクトを永続化するためのテストである私の主な方法であります

最後に私の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>stackoverflow_heplme</groupId> 
    <artifactId>sga</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-VIVALDI</version> <!-- Haha, spring. --> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.1.RELEASE</version> 
    </parent> 

    <properties> 
     <java-version>1.8</java-version> 
     <hibernate.version>4.3.6.Final</hibernate.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 

     <dependency> 
      <groupId>com.google.appengine.orm</groupId> 
      <artifactId>datanucleus-appengine</artifactId> 
      <version>2.1.2</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>javax.persistence</groupId> 
      <artifactId>persistence-api</artifactId> 
      <version>1.0</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>${hibernate.version}</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.hibernate.javax.persistence</groupId> 
        <artifactId>hibernate-jpa-2.1-api</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-c3p0</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
      <exclusions> 
       <exclusion> 
        <groupId>org.hamcrest</groupId> 
        <artifactId>hamcrest-core</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

     <dependency> 
      <groupId>org.hamcrest</groupId> 
      <artifactId>hamcrest-all</artifactId> 
      <version>1.3</version> 
      <scope>test</scope> 
     </dependency> 

    </dependencies> 


    <build> 
     <finalName>sga</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>${java.version}</source> 
        <target>${java.version}</target> 
        <compilerArgument>-Xlint:all</compilerArgument> 
        <showWarnings>true</showWarnings> 
        <showDeprecation>true</showDeprecation> 

       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

</project> 
+0

可能な重複を追加(https://stackoverflow.com/questions/13584269/java-lang-classnotfoundexception-javax-persistence-spi-persistenceprovider) – JFPicard

+0

この文脈では、classpathはpomファイルにあるすべてのものを参照します。 –

答えて

0

まず、persistence.xmlを作成する必要はありません.hibernateを使用したい場合、springbootバンドルにもpersistence providerが依存関係の下にbackroundを生成するので、persistence.xmlも必要ありません.pom .xmlとapplication.properties。

org.springframework.boot 春ブート・スターター・データ-JPA

[にjava.lang.ClassNotFoundException:javax.persistence.spi.PersistenceProvider]のこれはあなたのpom.xml

0

1.0の代わりにjpa 2.1の依存関係を追加しようとしましたか?

関連する問題