gpt4 book ai didi

java - 发现异常 javax.persistence.PersistenceException : No resource files named META-INF/services/javax. persistence.spi.PersistenceProvider

转载 作者:行者123 更新时间:2023-12-01 10:03:15 25 4
gpt4 key购买 nike

你好,我正在学习 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

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

Exception in thread "main" javax.persistence.PersistenceException: No resource files named META-INF/services/javax.persistence.spi.PersistenceProvider were found. Please make sure that the persistence provider jar file is in your classpathat javax.persistence.Persistence.findAllProviders(Persistence.java:167) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83) at 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>

持久化 xml 位于该目录中:

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

请帮我解决这个问题

最佳答案

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

关于java - 发现异常 javax.persistence.PersistenceException : No resource files named META-INF/services/javax. persistence.spi.PersistenceProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36651702/

25 4 0