2010-09-10 74 views
2

更新3: addes下面的代碼給pom所以openjpa可以找到persistence.xml文件。有一些查詢錯誤,但我終於得到了openjpa工作:)。Openjpa maven插件錯誤

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

UPDATE2:設置Maven插件在我的POM OpenJPA的。在運行我的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中不包括在戰爭文件。仍然從eclipse運行本地測試也不起作用。在戰爭中手動放置persitence.xml會導致下一個錯誤。我的猜測是我錯過了我的一些目標。我只在我的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(並部署有我的戰爭文件),我得到這個消息。 這是我的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> 
+0

在TRACE級別激活日誌記錄以查看是否獲得更多有用的信息:' – 2010-09-10 16:17:19

回答

2

我確實有包含的persistence.xml和openjpa.xml我的源文件夾名爲META-INF文件夾中。

這其中的資源都應該使用Maven的時候是不是,資源應該在src/main/resources即東西是這樣的:

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

如果按照約定,沒有什麼可以做的。如果你不這樣做,你需要添加一些明確的配置(包括測試)。我建議使用默認值。

+0

感謝您的信息。原來的項目不是一個maven項目,並有其他一些結構。我正在重構它。 – 2010-09-10 18:47:30

+0

@Mark不客氣。很高興它是有用的。 – 2010-09-10 19:04:34

+0

你的解決方案適用於maven/openjpa,但eclipse不再高興。 Eclipse期望構建路徑中的文件。我可以用eclipse中的一些偏好來抑制錯誤,但是我找不到選項來設置正確的路徑,所以eclipse可以驗證xml文件。有什麼想法? – 2010-09-10 19:27:48