Search code examples
jpamavenjpa-2.0openjpaarchetypes

Maven archetypes for OpenJPA


Greetings.

I'm just starting to explore Maven and I use m2eclipse as to use Maven in Eclipse.

I found that there is a hibernate-based archetype with Group Id: com.rfc.maven.archetypes and Artifact Id: jpa-maven-archetype

Does anybody knows if there are archetypes for OpenJPA-based projected with test frameworks included?

Thanks a lot.


Solution

  • Does anybody knows if there are archetypes for OpenJPA-based projected with test frameworks included?

    Not to my knowledge. So my suggestion would be to use the jpa-maven-archetype and to tweak it for OpenJPA and JPA 2.0.

    First, generate a project:

    $ mvn archetype:generate \
      -DgroupId=com.stackoverflow \
      -DartifactId=Q4161012 \
      -DpackageName=com.stackoverflow.domain \
      -DarchetypeGroupId=com.rfc.maven.archetypes \
      -DarchetypeArtifactId=jpa-maven-archetype  \
      -DarchetypeVersion=1.0.0 \
      -DremoteRepositories=http://maven.rodcoffin.com/repo \
      -DinteractiveMode=false
    

    Then cd into the created directory and modify the pom.xml to replace Hibernate by OpenJPA artifacts, add the OpenJPA plugin for enhancement (I did a few other minor tweaks):

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.stackoverflow</groupId>
      <artifactId>Q4161012</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>JPA Project</name>
      <properties>
        <openjpa.version>2.0.1</openjpa.version>
        <slf4j.version>1.6.1</slf4j.version>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.8.2</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.dbunit</groupId>
          <artifactId>dbunit</artifactId>
          <version>2.4.8</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.apache.openjpa</groupId>
          <artifactId>openjpa</artifactId>
          <version>${openjpa.version}</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
        <dependency>
          <groupId>hsqldb</groupId>
          <artifactId>hsqldb</artifactId>
          <version>1.8.0.7</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
              <includes>com/stackoverflow/domain/**/*.class</includes>
              <addDefaultConstructor>true</addDefaultConstructor>
              <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
              <!-- Pass additional properties to the Plugin here -->
              <toolProperties>
                <property>
                  <name>directory</name>
                  <value>otherdirectoryvalue</value>
                </property>
              </toolProperties>
            </configuration>
            <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>
                <version>${openjpa.version}</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    </project>
    

    Then modify the persistence.xml for JPA 2.0 and add the OpenJPA specific properties:

    <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0">
        <persistence-unit name="Q4161012"></persistence-unit>
        <persistence-unit name="Q4161012-test"
            transaction-type="RESOURCE_LOCAL">
            <class>com.stackoverflow.domain.User</class>
            <properties>
                <property name="javax.persistence.jdbc.driver"
                    value="org.hsqldb.jdbcDriver" />
                <property name="javax.persistence.jdbc.url"
                    value="jdbc:hsqldb:mem:my-project-test" />
                <property name="javax.persistence.jdbc.user" value="sa" />
                <property name="javax.persistence.jdbc.password" value="" />
    
                <property name="openjpa.jdbc.DBDictionary"
                    value="org.apache.openjpa.jdbc.sql.HSQLDictionary" />
                <property name="openjpa.jdbc.SynchronizeMappings"
                    value="buildSchema(SchemaAction=add)"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    And replace the following lines in UserTest.java (and clean up imports):

    HibernateEntityManager em = (HibernateEntityManager) emf.createEntityManager();
    
    DbUnitDataLoader loader = new DbUnitDataLoader(testData, em.getSession().connection());
    

    By (OpenJPA doesn't support the EntityManager#unwrap(Object) from JPA 2.0 yet, see OPENJPA-1803, so you have to use OpenJPA specific classes):

    EntityManager em = emf.createEntityManager();
    OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
    Connection conn = (Connection) oem.getConnection(); 
    conn.setAutoCommit(true);
    
    DbUnitDataLoader loader = new DbUnitDataLoader(testData, conn);
    

    And run the test:

    $ mvn clean test
    [INFO] Scanning for projects...
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    ...