java - Openjpa maven插件错误

标签 java jpa openjpa

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

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

更新2:在我的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 运行本地测试也不起作用。手动将 persistence.xml 放入 war 中会出现下一个错误。我的猜测是我错过了我的 pom 中的一些目标。我只在我的 pom 中得到了与 openjpa 相关的内容。

<!-- 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>

最佳答案

I do have a folder named META-INF in my source folder containing persistence.xml and openjpa.xml.

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

.
|-- src
|   |-- main
|   |   |-- java
|   |   |   `-- ...
|   |   `-- resources
|   |       `-- META-INF
|   |           |-- openjpa.xml
|   |           `-- persistence.xml
|   `-- test
|       |-- java
|       |   `-- ...
|       `-- resources
|           `-- ...
`-- pom.xml

如果你遵循惯例,那就没什么可做的了。如果不这样做,您将需要添加一些显式配置(包括测试配置)。我建议使用默认值。

关于java - Openjpa maven插件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3685113/

相关文章:

java - JPA跨单向OneToOne

java - 如何在 JSF 中填充数据

java - 无法让 ObjectInputStream 工作

Java编码——有多个正确答案的提示

java - 如何使用接口(interface)和 JPA

java - 如何阻止持久性更改数据库、JPA

java - 如何使用 BufferedWriter 编写 XML 标签

java - 如何使用 Hibernate 获取两个字段的唯一键?

java - JPA 多对多关系与额外的枚举列

java - JPA2 Lazy Child Collection with orphanRemoval=true 删除 'not-fetched' child