2010-09-10 58 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