OpenJPA的Maven原型 [英] Maven archetypes for OpenJPA

查看:103
本文介绍了OpenJPA的Maven原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候.

我刚刚开始探索Maven,并且使用m2eclipse就像在Eclipse中使用Maven.

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

我发现有一个基于休眠的原型, 组ID:com.rfc.maven.archetypes和 工件ID:jpa-maven-archetype

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

有人知道包含测试框架的基于OpenJPA的原型是否存在吗?

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

非常感谢.

推荐答案

有人知道包含测试框架的基于OpenJPA的原型是否存在吗?

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

据我所知.因此,我的建议是使用jpa-maven-archetype并针对OpenJPA和JPA 2.0进行调整.

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.

首先,生成一个项目:

$ 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

然后cd进入创建的目录,并修改pom.xml以用OpenJPA构件替换Hibernate,添加OpenJPA插件以进行增强(我做了一些其他小的调整):

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>

然后修改JPA 2.0的persistence.xml并添加OpenJPA特定的属性:

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>

并替换UserTest.java中的以下行(并清理导入):

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());

通过(OpenJPA尚不支持JPA 2.0中的EntityManager#unwrap(Object),请参见 OPENJPA-1803 ,因此您必须使用OpenJPA特定的类):

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

并运行测试:


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

这篇关于OpenJPA的Maven原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆