Mysql maven jpa skeleton
is there a skeleton for a project using mysql, some eclipse/top link with RESOURCE_LOCAL as connection type? Preferably using maven. I'm searching for it for hours and can't get running even the sipmlest exaple. So if you had it ready and running, please, post :-). Even something as simple as these two classes only.
@Entity
public class Message implements Serializable{
public Message() {}
p开发者_运维知识库ublic Message(String s){
this.s = s;
}
@Id
String s;
public String getS(){
return s;
}
}
public class App {
static private EntityManagerFactory emf;
static private EntityManager em;
public static void main( String[] args ) {
emf = Persistence.createEntityManagerFactory("persistence");
em = emf.createEntityManager();
Message m = new Message("abc");
em.persist(m);
}
}
Maven has an archetype for JPA Application that is available from the internal catalog
$ mvn archetype:generate [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:generate] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Preparing archetype:generate [INFO] No goals needed for project - skipping [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] [archetype:generate {execution: default-cli}] [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype: ... 23: internal -> jpa-maven-archetype (JPA application) ... Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41) 15: :
Or more directly:
mvn archetype:generate \
-DgroupId=com.mycompany.myproject \
-DartifactId=my-project-domain \
-DpackageName=com.mycompany.myproject.domain \
-DarchetypeGroupId=com.rfc.maven.archetypes \
-DarchetypeArtifactId=jpa-maven-archetype \
-DarchetypeVersion=1.0.0 \
-DremoteRepositories=http://maven.rodcoffin.com/repo \
-DinteractiveMode=false
This will generate a nice project using Hibernate, HSQLDB with a DbUnit test. Now, cd
into the created directory to make some changes.
First of all, edit the pom.xml
. Remove the Hibernate related artifacts and replace them by EclipseLink (you'll need an extra repository):
<project>
...
<repositories>
<repository>
<id>eclipselink</id>
<url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo/</url>
</repository>
</repositories>
<dependencies>
<!-- See http://wiki.eclipse.org/EclipseLink/Maven -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.0.0</version>
</dependency>
<!-- optional - only needed if you are using JPA outside of a Java EE container-->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
...
</project>
Then modify the persistence.xml
(note that there are 2 persistence units below and the one filled is used during tests):
<?xml version="1.0"?>
<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="my-project-domain"/>
<persistence-unit name="my-project-domain-test" transaction-type="RESOURCE_LOCAL">
<class>com.mycompany.myproject.User</class>
<properties>
<property name="eclipselink.target-database" value="HSQL"/>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:my-project-test"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
That's all. I've posted the modified pom.xml
on pastebin.com. To use MySQL, add the JDBC driver as dependency in the pom.xml
and replicate the EclipseLink configuration. This should be easy and is left as an exercise for the reader :) In case of a blocking problem, leave a comment and I'll see if I can help.
If you want to make the test pass again, you'll have to replace the following lines in UserTest.java
:
HibernateEntityManager em = (HibernateEntityManager) emf.createEntityManager();
DbUnitDataLoader loader = new DbUnitDataLoader(testData, em.getSession().connection());
By
EntityManager em = emf.createEntityManager();
Connection conn = em.unwrap(java.sql.Connection.class);
conn.setAutoCommit(true);
DbUnitDataLoader loader = new DbUnitDataLoader(testData, conn);
精彩评论