OpenJPA, Derby, and getting started fast….

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>
About these ads

7 thoughts on “OpenJPA, Derby, and getting started fast….

  1. Digambar says:

    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

  2. Tony Diep says:

    Thanks for the tutorial. It’s much, much clearer than every other tutorial I’ve tried over the weekend!

  3. Gadi says:

    Thanks a lot.
    You saved me a lot of time researching and configuring.
    well done :)

  4. Mike says:

    Konkurs przypuszczalnie traktować wyraźnie samych osób (ich wiedzy, zdolności, osiągnięć itp.
    ) czy rzeczy a także Mike kwestii, które zezwalają fajne konkursy wyłącznie rezultatem funkcjonowanie osoby ludzkiej (kryterium przetrwania:
    najatrakcyjniejszym, najciekawszym, w największym
    stopniu popularnym, w najwyższym stopniu zupełnym
    itp. ). Uczestników konkursu nie winno oficjalnie powołać
    jego organizator, fajne konkursy wskazana dzięki niego komisja to znaczy instytucja.

  5. Thank you a bunch for sharing this with all people you really recognize
    what you are talking about! Bookmarked. Please also talk over with my site =).
    We can have a hyperlink exchange agreement between us

  6. Lenard says:

    I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored material
    stylish. nonetheless, you command get got an nervousness over that you wish be delivering
    the following. unwell unquestionably come more formerly again as exactly
    the same nearly very often inside case you shield
    this hike.

  7. Nila says:

    Naprawdę Nila cóż. Ostre kubki, budynie, Nila kostki rosołowe, kaszki i tym podobne owo też chemia.
    Tudzież stosujemy urywa tudzież to po wielokroć.
    Składy słodyczy również nie przebywają doskonałe.
    Zdaje się lody to sama chemia, zaś ja piłuje lubię natomiast Nila
    jadam. Rozumuję, że dwa tygodnie takiej diety mi nie zaszkodziły Nila, tudzież pomogły wystarczająco bezboleśnie wgramolić się w nowy zwyczaj odżywiania.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: