gpt4 book ai didi

jpa - OpenJPA 的 Maven 原型(prototype)

转载 作者:行者123 更新时间:2023-12-04 21:56:32 25 4
gpt4 key购买 nike

问候。

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

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

有谁知道是否有包含测试框架的基于 OpenJPA 的原型(prototype)?

非常感谢。

最佳答案

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

据我所知不是。所以我的建议是使用 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

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

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

通过(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] ------------------------------------------------------------------------...

关于jpa - OpenJPA 的 Maven 原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4161012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com