I am building a maven web project ( jsf+ejb3+jpa2) named " tuto.maven" using eclipse kepler, glassfish4.0, postgresSql 9.1, but where I try to run the project on a server a problem occurs
"cannot Deploy tuto.maven
deploy is failing=Error occurred during deployment: Exception while preparing the app : java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence "
Here is the persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="persistence" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/postgres</jta-data-source>
<class>org.model.User</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
and pom.xml :
<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>org.tuto</groupId>
<artifactId>tuto.maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>oss.sonatype.org</id>
<name>OSS Sonatype Staging</name>
<url>https://oss.sonatype.org/content/groups/staging</url>
</repository>
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.0.1B</version>
</dependency>
</dependencies>
</project>
any idea please .
It was a problem of version, I use the latest version and it works.
Make sure you have the jar's and search for prg.hibernate.ejb.HibernatePersistence class in your jars.
You also need to add in your persistence unit. Mention the name of the domain class.
<class>yourpackage.class</class>
Related
So I'm trying to test a maven project using Arquilian on Eclipse for a school project, however when I try to run the test through mvn clean test I get the following stacktrace:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.361 sec {"Operation step-1" => {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"testCapAnalogique.war\".POST_MODULE" => "WFLYSRV0153: Failed to process phase POST_MODULE of deployment \"testCapAnalogique.war\"
Caused by: java.lang.RuntimeException: WFLYSRV0177: Error getting reflective information for class fr.esisar.locVoiture.stateless.CapAnalogiqueStateless with ClassLoader ModuleClassLoader for Module \"deployment.testCapAnalogique.war\" from Service Module Loader
Caused by: java.lang.NoClassDefFoundError: Failed to link fr/esisar/locVoiture/entities/CapAnalogique (Module \"deployment.testCapAnalogique.war\" from Service Module Loader): fr/esisar/locVoiture/entities/Capteur"}}}}
I'm using the following arquillian.xml:
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0" />
<container qualifier="jboss" default="true">
<configuration>
<property name="jbossHome">home/user/CS513-Archive/wildfly-
16.0.0.Final</property>
</configuration>
</container>
</arquillian>
And the following test-persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="test">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
and the test-ds.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<!-- The datasource is bound into JNDI at this location. We reference
this in META-INF/test-persistence.xml -->
<datasource jndi-name="java:jboss/datasources/BeanValidationQuickstartTestDS"
pool-name="bean-validation-quickstart-test" enabled="true"
use-java-context="true">
<connection-url>jdbc:h2:mem:bean-validation-quickstart-test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
with the dependencies in pom.xml:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.13.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>arq-managed</id>
<dependencies>
<dependency>
<groupId>org.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-container-managed</artifactId>
<version>2.1.0.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>arq-remote</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-container-remote</artifactId>
<version>2.1.0.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Please note I'm new to Maven and arquillian and I may be missing something trivial.
So the problem was that I'm adding classes in my createArchiveTest() function as resources in the ShrinkWrap create method, that extend and implement other classes and interfaces. Turns out all of them actually need to be passed as resources.
Say you want to test a class called StudentA, but this class extends another class called Student and the Student class implements a Person interface, all three need to be passed as resources.
I have created a Spring starter project with eclipse STS tool after creating project there is an error in pom.xml first. But I found no error. I have tried Updating project with and with out forcefully but error still persists
Thank you
<?xml version="1.0" encoding="UTF-8"?> <!-- error occurs here -->
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.web-app</groupId>
<artifactId>FirstWebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FirstWebApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Project runs but not working.
[This is the image of eclipse workspace] [1]: https://i.stack.imgur.com/a9fGB.png
Well , this is the latest well known problem of Eclipse when working with Spring Boot 2.1.5.
The workaround is to add the following to pom.xml :
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
I added spring-webmvc to my maven dependencies.
According to some page I found I need to use this:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
It works fine and I can see the jar file in my m2 local repository.
But my problem is that I'm getting an error in my JSP file that says "The tag handler class for "form:form" (org.springframework.web.servlet.tags.form.FormTag) was not found on the Java Build Path".
It obviously is looking for org.springframework.web.servlet BUT I thought adding spring-webmvc would fix it as mentioned is several search results I've read. Plus I've used "org.springframework.web.servlet.view.UrlBasedViewResolver" in my spring bean configuration XML so I know it's loading parts of the spring-webmvc.
I've looked at repo1.maven.org and mvnrepository.com and I never saw them mentioned org.springframework.web.servlet.
I then decided to add web.servlet according to the springsource page (http://ebr.springsource.com/repository/app/bundle/version/detail?name=org.springframework.web.servlet&version=3.1.3.RELEASE)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web.servlet</artifactId>
<version>${org.springframework.version}</version>
</dependency>
But I then get this error "Missing artifact org.springframework:org.springframework.web.servlet:jar:3.1.3.RELEASE".
I added springsource own repo (http://ebr.springsource.com/repository/app/faq) to my pom.xml but I get the same "missing artifact" message.
I guess I'm stuck and need your help. thanks :)
Not sure if this is relevant but I've included 3 XML config files. :)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.flinders.staffdirectory.controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" >
<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.flinders.staff.directory</groupId>
<artifactId>directory-maven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>directory-maven Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<org.springframework.version>3.1.3.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<finalName>directory-maven</finalName>
</build>
</project>
I normally put this page directive at the top of the file:
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
Which should make sure that the form taglib is being loaded in the JSP.
Seems like I forgot to pass a modelAttribute to my form:
return new ModelAndView("search/SearchForm", "search", new Search());
I was able to resolve the issue by adding the taglib directive as the first line in the file.
Here again (I had to rewrite it to make it more comprehensive i guess):
I got project X and project Y. They are independant from each other. While project X is working with all hibernate dependencies, I get ClassNotFound Exceptions and NoClassDefFoundErrors on project Y with the same dependencies. The errors occur, because the hibernate jars seem not to be deployed on project Y. I have to put them jar by jar in the WEB-INF/lib folder that they get deployed for runtime, while in project X they are already deployed via dependencies. The difference: Project Y has a parent project.
Here the pom.xml s:
Working Project X:
<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>org.hibernate.tutorials</groupId>
<artifactId>hibernate-tutorial</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>First Hibernate Tutorial</name>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.35</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
Project Y:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>com-project</artifactId>
<groupId>com.project</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.project.core</groupId>
<artifactId>com-project-core</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>com-project-core Jersey Webapp</name>
<dependencies>
<dependency>
<groupId>com.project.facade</groupId>
<artifactId>com-project-facade</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.35</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
</dependencies>
</project>
Project Y parent pom:
<?xml version="1.0" encoding="UTF-8"?>
<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.project</groupId>
<artifactId>com-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>com-project</name>
<url>http://maven.apache.org</url>
<modules>
<module>com-project-core</module>
<module>com-project-client</module>
<module>com-project-facade</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I already tried this attempt, but its all set.
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.
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] ------------------------------------------------------------------------
...