Now, some time ago I wrote about a project I'm doing in my spare time, it's called xkcdreader and is an example of how StAX, persistency and some network stuff can be used in java. Since I began the project I chose db4o for persistency. Db4o is an object database, which means it stores objects directly in the database without mapping, this is however not what the majority of databases today use. In the world today most dbms systems are relational, this is where JPA (Java Persistence API) comes into the picture.

JPA is a specification for mapping objects into relational databases and back again, this is normally known as Object/Relational-Mapping (or ORM for short). The reference implementation of the JPA specification is known as EclipseLink, EclipseLink is build on the old Oracle TopLink codebase. The new JPA 2.0 specification is not bound to EJB 3.0 (Enterprise JavaBeans) like the 1.0 specification was.

I took the old xkcdreader project and converted the database backend from using db4o to use JPA2 with EclipseLink in something like 5-10 minutes, all I did was annotate the Comic class, make a persistence.xml with a persistence-unit and implement the XkcdDao interface with the EntityManager from EclipseLink.

One of the backsides of using JPA2 and EclipseLink is that it's around 5-6 mb in size, then you can add the size of the jdbc driver on top of that. I know it's not alot, but compared to db4o or just a jdbc driver, it's quiet a difference.

For the backend I chose SQLite, since it's embedded, it uses SQL, it's rather light, I do alot of my other programming with it, and it's not particular bound to any platform. The SQLite JDBC driver I use is the one found here.

Here's the persistence.xml I made in the short timeframe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<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="Comics" transaction-type="RESOURCE_LOCAL">
 <class>model.data.Comic</class>
 <properties>
 <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
 <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:xkcdreader.db" />
 <property name="eclipselink.logging.level" value="WARNING" />
 <property name="eclipselink.ddl-generation" value="create-tables" />
 </properties>
 </persistence-unit>
</persistence>

The driver is just the jdbc-driver, the url is the connection string used by DriverManager.getConnection(), logging and ddl-generation is specific to eclipselink, logging level is pretty self explaning, ddl-generation makes it possible to generate tables automatically. Some of the properties that I did not specify (for JPA) is user and password.

Some mandatory links: