【问题标题】:Maven/Eclipse: Could not find any META-INF/persistence.xml file in the classpathMaven/Eclipse:在类路径中找不到任何 META-INF/persistence.xml 文件
【发布时间】:2014-07-13 04:21:55
【问题描述】:

我知道关于这个问题还有其他关于 SO 的问题,但没有一个解决方案对我有用。我正在使用 maven 在 eclipse 中构建一个 java 项目,并且我的 persistence.xml 文件位于 src/main/resource/META_INF 文件夹中。但是当我尝试 mvn install 时,我总是得到这个错误:

No Persistence provider for EntityManager named plasma.persistence

查看控制台输出似乎是由以下原因引起的:

[org.hibernate.jpa.boot.internal.PersistenceXmlParser] - HHH000318: Could not find any META-INF/persistence.xml file in the classpath

这是我的 persistence.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_2_0.xsd"
    version="2.0">
<persistence-unit name="plasma.persistence" transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432:xxxxx"/>
        <property name="hibernate.connection.username" value="xxxxx"/>
        <property name="hibernate.connection.password" value="xxxxx"/>
        <property name="hibernate.connection.pool_size" value="5"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.max_fetch_depth" value="5"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

我已尝试右键单击项目并将 META_INF 文件夹添加到构建路径,但仍然无法正常工作。有什么想法吗?

【问题讨论】:

    标签: java eclipse maven


    【解决方案1】:

    持久性文件的正确位置应该是 src/main/resources/META-INF。在问题中,您提到了 src/main/resource/META_INF。请注意,资源中应该有一个“s”,而 META-INF 中应该有一个破折号 (-),而不是下划线。这些只是问题中的错别字吗?如果没有,请修复路径,它应该可以工作。

    【讨论】:

      【解决方案2】:

      我放了一个排除,maven 停下来放persistence.xml。我不得不用蛮力来修复:

          <resources>
              <!-- After this maven stopped to put parsistence.xml-->
              <resource>
                  <directory>src/main/resources/CATALINA_HOME/conf</directory>
                  <excludes>
                      <exclude>log4j.properties</exclude>
                      <exclude>jpa_stocks.properties</exclude>
                  </excludes>
              </resource>
              <!-- Brutal force to fix -->
              <resource>
                  <directory>src/main/resources/META-INF</directory>
                  <targetPath>META-INF</targetPath>
                  <includes>
                      <include>persistence.xml</include>
                  </includes>
              </resource>
          </resources>
      

      【讨论】:

      • 请注意,这不是排除,而是&lt;resource&gt; 已被定义。一旦添加了任何资源元素,开发人员就可以明确定义所需的一切。不再使用 Maven 默认值。这就是为什么在上面的示例中需要第二个&lt;resource&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 2014-09-22
      • 1970-01-01
      相关资源
      最近更新 更多