Search code examples
javamavenopenjpa

maven openjpa sql generation issue


I am trying to add the sql generator goal to my openjpa plugin. I use OpenJPA 2.2.0. I have the jpa enhancer working with the following relevant part of the pom.sql file:

<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>

As I then try to add the following to the executions part of the openjpa plugin:

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

I get the following error while executing mvn -e install command: http://pastebin.com/TENgXezJ

A shorter version from running without the -e switch:

[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]

What am I missing?


Solution

  • OK, so I solved it myself. Apparently the trick is not inside the pom.xml but rather in the persistence unit definition in persistence.xml

    It is really easy. I just added

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

    And I was done.

    Apparently for OpenJPA you also need to list the entity classes you wish to have generated according to the information here: http://planet.jboss.org/post/generate_a_database_schema_with_openjpa_and_hibernate_on_jboss_as_7

    As I had already listed my classes previously I didn't recognize this as a needed step.