2012-11-01 49 views
2

我想將sql生成器的目標添加到我的openjpa插件。我使用OpenJPA 2.2.0。我有JPA增強與pom.sql文件的下列相關部分工作:maven openjpa sql生成問題

<build> 

    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.openjpa</groupId> 
       <artifactId>openjpa-maven-plugin</artifactId> 
       <version>${org.apache.openjpa.version}</version> 
       <configuration> 
        <includes>org/someorg/models/local/*.class</includes> 

        <addDefaultConstructor>true</addDefaultConstructor> 
        <connectionDriverName>org.postgresql.Driver</connectionDriverName> 
        <enforcePropertyRestrictions>true</enforcePropertyRestrictions> 
        <sqlFile>src/main/resources/sql/create.sql</sqlFile> 
       </configuration> 
      </plugin> 
     </plugins> 
    </pluginManagement> 

    <plugins> 

     <plugin> 
      <groupId>org.apache.openjpa</groupId> 
      <artifactId>openjpa-maven-plugin</artifactId> 
      <version>${org.apache.openjpa.version}</version> 

      <executions> 
       <execution> 
        <id>enhancer</id> 
        <phase>process-classes</phase> 
        <goals> 
         <goal>enhance</goal> 
        </goals> 
       </execution>      
      </executions> 
      <dependencies> 
       <dependency> 
        <groupId>org.apache.openjpa</groupId> 
        <artifactId>openjpa</artifactId> 
        <!-- set the version to be the same as the level in your runtime --> 
        <version>${org.apache.openjpa.version}</version> 
       </dependency> 
      </dependencies> 
     </plugin> 
</plugins> 

當我再嘗試添加以下OpenJPA的插件的執行部分:

<execution> 
    <id>sql</id> 
    <phase>generate-resources</phase> 
    <goals> 
     <goal>sql</goal> 
    </goals> 
</execution> 

我出現以下錯誤而執行MVN -e install命令: http://pastebin.com/TENgXezJ

從沒有-e開關運行的一個較短的版本:

[ERROR] 
Failed to execute goal org.apache.openjpa:openjpa-maven-plugin:2.2.0:sql(sql) on project batchpoc: 
Execution sql of goal org.apache.openjpa:openjpa-maven-plugin:2.2.0:sql 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] 
[ERROR] 

我錯過了什麼?

+0

錯誤消息明確指出它無法找到'persistence.xml'。你把它放在'META-INF'中嗎? – maba

+0

@maba是的,它駐留在src/main/resources/META-INF中,它應該工作,因爲我可以增強類或者是與persistence.xml無關的增強功能? – Rythmic

回答

2

好的,我自己解決了。顯然這個技巧不在pom.xml中,而是在persistence.xml中的持久化單元定義中。

這很簡單。我剛加入

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/> 

我完成了。

顯然,關於OpenJPA你還需要列出你希望有根據的信息在這裏產生的實體類: http://planet.jboss.org/post/generate_a_database_schema_with_openjpa_and_hibernate_on_jboss_as_7

正如我已經列出我的課之前我不承認這是一個必要的步驟。