在类路径中找不到任何META-INF / persistence.xml文件

时间:2017-08-09 14:42:45

标签: java xml eclipse hibernate jpa

我尝试将JPA设置为我的RESTful webapi,以便在数据库上创建CRUD服务。我收到错误Could not find any META-INF/persistence.xml file in the classpath但实际上是在文件夹META-INF中的persistence.xml中

1 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.6-Final
    17 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.6-Final
    20 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
    23 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
    27 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
    110 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
    115 [main] INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.5.6-Final
    129 [main] INFO org.hibernate.ejb.Ejb3Configuration - Could not find any META-INF/persistence.xml file in the classpath
    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named KAPAPLAN
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
        at de.ham.ti.kapaplan.database.DBCon.main(DBCon.java:20)

这里是我的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="KAPAPLAN" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>de.ham.ti.kapaplan.model</class>

    <properties>
      <!-- Configuring JDBC properties -->
      <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@//***db-domain***/***service-name***" />
      <property name="javax.persistence.jdbc.user" value="***user***" />
      <property name="javax.persistence.jdbc.password" value="***pw***" />
      <property name="javax.persistence.jdbc.driver" value="com.mysema.query.jpa.support.ExtendedOracleDialect" />

      <!-- Hibernate properties -->
      <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" />

      <!-- Configuring Connection Pool -->
      <!-- <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>

我仔细检查了类似的问题,但没有解决我的问题。安娜的想法?

编辑:我的Maven 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>de.ham.ti.kapaplan</groupId>
    <artifactId>kapaplan</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>kapaplan</name>

    <build>
        <finalName>kapaplan</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.16</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

3 个答案:

答案 0 :(得分:0)

您需要将persistence.xml移动到应用服务器的类路径上的某个位置。对于Maven项目,通常位于src/main/resources文件夹中。检查Hibernate文档以查看Hibernate是否希望文件位于子文件夹中,或者将其放在类路径的根目录中是否正常。

答案 1 :(得分:0)

我认为您的项目配置错误。 META-INF文件夹应保留在Web maven项目的src/main/webapp中的src / main / resources和WEB-INF文件夹中。这是一个example我在网上api做了一些CRUD。

答案 2 :(得分:0)

在Maven项目中设置META-INF路径的最简单方法是使用pom.xml并使用目录标记直接在该位置设置路径。我面临着同样的问题,但使用此解决了。 例如:-您的META-INF位于src文件夹中 您的pom.xml将为:

 <dependencies>
 ..
 </dependencies>
 <build>
   
   <resources>
     <resource>
       <directory>E:/Netbeans Repo/Hibernate/src</directory>
     </resource>
   </resources>

 

 </build>

这应该有效