更新 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)
起居室:
<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>