异常javax.persistence.PersistenceException:找不到名为META-INF / services / javax.persistence.spi.PersistenceProvider的资源文件

时间:2016-04-15 15:53:55

标签: java maven jpa eclipselink

你好我正在学习java ee和maven这里是我的代码 主文件:

public class Main {
    public static void main(String[] args) {
        Book livre = new Book();
        livre.setId(new BigDecimal("1"));
        livre.setDescription(" la chanson dans la foret  avec coldplay ");
        livre.setIsbn("12.5.8");
        livre.setNbofpage(new BigInteger("2354"));
        livre.setTitle("adventure of a lifetime");
        livre.setPrice(new BigDecimal("267"));
        //creation de l'objet
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BookStorePU"); 
        EntityManager em = emf.createEntityManager();
        EntityTransaction utx = em.getTransaction();
        utx.begin();
        em.persist(livre);
        utx.commit();
        TypedQuery<Book> crna = em.createNamedQuery("Book.findAll", Book.class);
        List<Book> livres = crna.getResultList();
        System.out.println(livres.toString());
    }
}

pom xml:       http://maven.apache.org/xsd/maven-4.0.0.xsd">     4.0.0

<groupId>com.espoirmur</groupId>
<artifactId>BookStoreApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>BookStoreApp</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>commonj.sdo</artifactId>
            </exclusion>
        </exclusions>
           <scope>provided</scope>

    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.1</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这是项目结构:     netbeans structure

project file directory

清理并构建并运行主文件后,我得到:

  

线程“main”中的异常javax.persistence.PersistenceException:否   资源文件命名   META-INF / services / javax.persistence.spi.PersistenceProvider是   找到。请确保持久性提供程序jar文件位于   你的班级   javax.persistence.Persistence.findAllProviders(Persistence.java:167)     在   javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103)     在   javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)     在com.espoirmur.Entity.Main.main(Main.java:30)

持久性.xml文件:

<persistence-unit name="BookStorePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
        <property name="javax.persistence.jdbc.driver value="oracle.jdbc.OracleDriver" />
        <property name="javax.persistence.jdbc.url" 
            value="jdbc:oracle:thin:@localhost:1521:XE" />
        <property name="javax.persistence.jdbc.user" value="espoir" />
        <property name="javax.persistence.jdbc.password" value="9874" />
    </properties>
</persistence-unit>

persistene xml在该目录中:

C:\Users\Espoir M\Google Drive\BookStoreApp\src\main\resources\META-INF

请帮我解决这个问题

2 个答案:

答案 0 :(得分:1)

亲爱的Espoir JTA只有在.war或.ear项目作为Glassfish运行到JEE contenait时才能用于主java类。要运行代码,请创建一个简单的java项目,并尝试使用RESOURCE_LOCAL作为事务类型。

答案 1 :(得分:0)

根据Celestin的回答我改变了我的持久性xml的第一行,如下所示:

 <persistence-unit name="BookStorePU" transaction-type="RESOURCE_LOCAL">