The combination of Apache OpenJPA and Derby provides a fantastic and simple way to prototype database apps in Java. The only downside is that there are a couple of gotchas I keep on finding myself hitting each time I start a new project. Here’s the simplest app I can think of and all the associated files for my later use. If it’s useful to you as well, have fun!
File: src/main/java/test/Account.java
package test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Account {
private String name;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
public Account(String name) {
this.name = name;
}
public String getName() {
return name;
}
public long getId() {
return id;
}
}
File: src/main/java/META-INF/persistence.xml
<persistence version="1.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_1_0.xsd"> <persistence-unit name="account"> <class>test.Account</class> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> <property name="openjpa.ConnectionURL" value="jdbc:derby:testdb;create=true" /> <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" /> </properties> </persistence-unit> </persistence>
File: src/test/java/test/AccountTest.java
package test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import junit.framework.TestCase;
public class AccountTest extends TestCase {
public void testAccount() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("account");
EntityManager em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
et.begin();
Account a = new Account("Bob");
em.persist(a);
et.commit();
System.out.println("a"+a.getId());
}
}
File: 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>test</groupId> <artifactId>test</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>test</name> <dependencies> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-all</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version>10.5.3.0_1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <type>jar</type> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>openjpa-maven-plugin</artifactId> <version>1.0</version> <configuration> <includes>**/entities/*.class</includes> <excludes>**/entities/XML*.class</excludes> <addDefaultConstructor>true</addDefaultConstructor> <enforcePropertyRestrictions>true</enforcePropertyRestrictions> </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>2.0.0</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
Advertisement

the best example I can say…
I took 2 days to do these configurations on my own and then I found out this link. This is the most lean way to try jpa api
Thanks for the tutorial. It’s much, much clearer than every other tutorial I’ve tried over the weekend!
Thanks a lot.
You saved me a lot of time researching and configuring.
well done