6

Is there any way to generate the JPA 2.0 metamodel via maven without having a persistence.xml file. I'm using eclipselink.

In my Java EE-projects I'm doing something like the following wich works fine because in that case I have a persistence.xml.

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.0.5</version>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <!-- there must no line break between the two compiler arguments! -->
                <compilerArguments>-Aeclipselink.persistencexml=${basedir}/src/main/resources/META-INF/persistence.xml</compilerArguments>
                <processors>
                    <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
            <version>2.3.2</version>
        </dependency>
    </dependencies>
</plugin>

Now I have a spring project which configures the jpa via spring context. Is the only way to create the metamodel to create a persistence.xml or can I somehow stay with the configuration in the spring context?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Christian Hager
  • 468
  • 6
  • 22

2 Answers2

1

I believe EclipseLink needs the persistence.xml anyway.

Please take a look at jpa-metamodel-with-maven.

You can use Hibernate, Apache OpenJPA, or DataNucleus in only the phase of metamodel generation.

Hibernate

<build>
  <plugins>
    <plugin>
      <groupId>org.bsc.maven</groupId>
      <artifactId>maven-processor-plugin</artifactId>
      <executions>
        <execution>
          <id>process</id>
          <goals>
            <goal>process</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <processors>
               <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
            </processors>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-jpamodelgen</artifactId>
          <version>${hibernate.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

Apache OpenJPA

<build>
  <plugins>
    <plugin>
      <groupId>org.bsc.maven</groupId>
      <artifactId>maven-processor-plugin</artifactId>
      <executions>
        <execution>
          <id>process</id>
          <goals>
            <goal>process</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <processors>
              <processor>org.apache.openjpa.persistence.meta.AnnotationProcessor6</processor>
            </processors>
            <optionMap>
              <openjpa.metamodel>true</openjpa.metamodel>
            </optionMap>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.apache.openjpa</groupId>
          <artifactId>openjpa</artifactId>
          <version>${openjpa.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

DataNucleus

<build>
  <plugins>
    <plugin>
      <groupId>org.bsc.maven</groupId>
      <artifactId>maven-processor-plugin</artifactId>
      <executions>
        <execution>
          <id>process</id>
          <goals>
            <goal>process</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <processors>
              <processor>org.datanucleus.jpa.query.JPACriteriaProcessor</processor>
            </processors>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.datanucleus</groupId>
          <artifactId>datanucleus-jpa-query</artifactId>
          <version>${datanucleus.version}</version>
        </dependency>
        <dependency>
          <groupId>org.datanucleus</groupId>
          <artifactId>datanucleus-core</artifactId>
          <version>${datanucleus.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
0

With the following example you don't need a persistence.xml and can "stay with the configuration in the spring context" like this:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.sample.company" />
    <property name="jpaPropertyMap">
        <map>
            <entry key="eclipselink.weaving" value="false"/>
            <entry key="eclipselink.jpa.uppercase-column-names" value="true"/>
            <entry key="eclipselink.ddl-generation" value="create-tables"/>
        </map>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" >
            <property name="databasePlatform" value="org.eclipse.persistence.platform.database.HSQLPlatform" />
            <property name="generateDdl" value="true" />
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

packagesToScan: Where to look for @Entity and other JPA annotations

jpaPropertyMap: Settings you would previously put in the persistence.xml

testing123
  • 11,367
  • 10
  • 47
  • 61
  • I know how to configure jpa via spring. What I fail to see is how your answer is going to help me generate the static metamodel without having a persistence.xml file. – Christian Hager Apr 13 '13 at 08:24
  • The Problem is that eclipselinks CanonicalModelProcessor doesn't seem to be able to work with anything else than the persistence.xml file. – Christian Hager Apr 13 '13 at 08:36
  • I see. Sorry about that I misread your question. I am not sure how to do that. – testing123 Apr 15 '13 at 19:53