更新 3: 次のコードを pom に追加して、openjpa が persistence.xml ファイルを見つけられるようにします。クエリエラーがいくつか残っていましたが、最終的にopenjpaが機能するようになりました:)。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
Update2: pom で 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 を war に手動で配置すると、次のエラーが発生します。私の推測では、ポンポンでいくつかの目標を逃したと思います。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>