【问题标题】:Openjpa maven plugin errorOpenjpa maven插件错误
【发布时间】:2011-04-10 18:01:24
【问题描述】:

更新 3: 将以下代码添加到 pom 中,以便 openjpa 可以找到 persistence.xml 文件。剩下一些查询错误,但我终于让 openjpa 工作了:)。

<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <include>**/*.xml</include>
    </includes>
  </resource>
</resources>

Update2: 在我的 pom.xml 中为 openjpa 设置 maven 插件。现在在运行我的 mavan 构建时遇到了一个不错的新错误。我的源文件夹中有一个名为 META-INF 的文件夹,其中包含 persistence.xml 和 openjpa.xml。

[ERROR] Failed to execute goal org.codehaus.mojo:openjpa-maven-plugin:1.1:enhance (enhancer) on project scarletreports: Execution enhancer of goal org.codehaus.mojo:openjpa-maven-plugin:1.1:enhance failed: MetaDataFactory could not be configured (conf.newMetaDataFactoryInstance() returned null). This might mean that no configuration properties were found. Ensure that you have a META-INF/persistence.xml file, that it is available in your classpath, or that the properties file you are using for configuration is available. If you are using Ant, please see the <properties> or <propertiesFile> attributes of the task's nested <config> element. This can also occur if your OpenJPA distribution jars are corrupt, or if your security policy is overly strict. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:openjpa-maven-plugin:1.1:enhance (enhancer) on project scarletreports: Execution enhancer of goal org.codehaus.mojo:openjpa-maven-plugin:1.1:enhance failed: MetaDataFactory could not be configured (conf.newMetaDataFactoryInstance() returned null). This might mean that no configuration properties were found. Ensure that you have a META-INF/persistence.xml file, that it is available in your classpath, or that the properties file you are using for configuration is available. If you are using Ant, please see the <properties> or <propertiesFile> attributes of the task's nested <config> element. This can also occur if your OpenJPA distribution jars are corrupt, or if your security policy is overly strict.
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:593)

pom:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>openjpa-maven-plugin</artifactId>
        <version>1.1</version>
        <configuration>
          <includes>**/jpa/**/*.class</includes>
          <addDefaultConstructor>true</addDefaultConstructor>
          <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
        </configuration>
        <executions>
          <execution>
            <id>enhancer</id>
            <phase>process-classes</phase>
            <goals>
              <goal>enhance</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

更新: 似乎persistence.xml 不包含在war 文件中。仍然从 Eclipse 运行本地测试也不起作用。在战争中手动放置 persitence.xml 会产生下一个错误。我的猜测是我错过了我的 pom 中的一些目标。我只在与 openjpa 相关的 pom 中得到了这个。

<!-- include open jpa for connection with rational databases -->
    <dependency>
      <groupId>org.apache.openjpa</groupId>
      <artifactId>openjpa-all</artifactId>
      <version>2.0.1</version>
    </dependency>

老问题:

我在 Web 应用程序中使用 openjpa 时遇到问题。我收到以下错误。

<openjpa-2.0.1-r422266:989424 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.
 org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:76)

我无法弄清楚为什么我会收到此消息,因为该属性是在配置中定义的,并且驱动程序包含在 maven 中(并且它与我的 war 文件一起部署)。 这是我的openjpa连接代码。

public class UserManagerFactory {
 private EntityManagerFactory emf;
 private EntityManager em;

 public void initEM() {
  emf = Persistence.createEntityManagerFactory("localDB");
  em = emf.createEntityManager();
 }

 public User getUser() {
  initEM();
  List<User> results = em.createQuery("select u from users as u", User.class).getResultList();
  closeEM();
  return results.get(0);
 }

 public void closeEM() {
  em.close();
  emf.close();
 }
}

这是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
 <persistence-unit name="localDB" transaction-type="RESOURCE_LOCAL">
  <class>package.User</class>
  <properties>
   <!-- enable warnings for debugging -->
   <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
   <!-- connection properties -->
            <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost/test"/>
            <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
            <property name="openjpa.ConnectionUserName" value="root"/>
            <property name="openjpa.ConnectionPassword" value=""/>

  </properties>
 </persistence-unit>
</persistence>

【问题讨论】:

  • 在 TRACE 级别激活日志记录以查看是否获得更多有用信息:&lt;property name="openjpa.Log" value="DefaultLevel=TRACE"/&gt;

标签: java jpa openjpa


【解决方案1】:

我的源文件夹中有一个名为 META-INF 的文件夹,其中包含 persistence.xml 和 openjpa.xml。

这不是使用 Maven 时资源应该放在的地方,资源应该放在 src/main/resources 下,即如下所示:

. |-- 源 | |-- 主要 | | |-- 爪哇 | | | `-- ... | | `-- 资源 | | `-- 元信息 | | |-- openjpa.xml | | `-- 持久性.xml | `-- 测试 | |-- 爪哇 | | `-- ... | `-- 资源 | `-- ... `-- pom.xml

如果您遵守约定,则无事可做。如果不这样做,则需要添加一些显式配置(包括测试)。我建议使用默认值。

【讨论】:

  • 感谢您的信息。最初的项目不是一个 Maven 项目并且有一些其他的结构。我马上重构它。
  • 您的解决方案适用于 maven/openjpa,但 eclipse 不再高兴了。 Eclipse 需要构建路径中的文件。我可以在 eclipse 中通过一些偏好来抑制错误,但我找不到设置正确路径的选项,因此 eclipse 可以验证 xml 文件。有什么想法吗?
  • @Mark 它也适用于 Eclipse。您使用什么进行 Maven 集成?
  • @Mark m2eclipse 绝对支持标准的 Maven 布局。更新您的项目配置(右键单击项目然后Maven > 更新项目配置)。
猜你喜欢
  • 2021-12-30
  • 1970-01-01
  • 2012-08-12
  • 2016-05-18
  • 2012-03-02
  • 2018-12-20
  • 1970-01-01
  • 2014-09-18
  • 2018-12-25
相关资源
最近更新 更多