OpenJPA 的 Maven 原型

发布于 2024-10-02 05:10:23 字数 205 浏览 5 评论 0原文

问候。

我刚刚开始探索 Maven,我使用 m2eclipse 就像在 Eclipse 中使用 Maven 一样。

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

有谁知道是否有包含测试框架的基于 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-10-09 05:10:23

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

据我所知。所以我的建议是使用 jpa-maven-archetype 并针对 OpenJPA 和 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插件进行增强(我做了一些其他细微调整):

<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 特定属性:

<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 中的以下行(并清理导入):

HibernateEntityManager em = (HibernateEntityManager) emf.createEntityManager();

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

By(OpenJPA 不尚不支持 JPA 2.0 中的 EntityManager#unwrap(Object),请参阅 OPENJPA-1803 ,所以你必须使用 OpenJPA 特定的类):

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

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] ------------------------------------------------------------------------
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文