OpenBooks: A sample JPA 2.0 Application


OpenBooks is a sample (and perhaps simple) application to demonstrate some of the new features of version 2.0 of Java Persistence API (JPA) specification, such as
  1. Creating dynamic Criteria Query from an input form
  2. Using compound, derived identity and orphan delete for modeling composite relationship
  3. Browsing Persistent Domain Model via MetaModel API

OpenBooks runs with OpenJPA as its JPA provider.

Instructions to download and run OpenBooks Demo

OpenBooks comes with
  1. complete source code
  2. build scripts to demonstrate how to build a typical OpenJPA application and package it for JSE or JEE environment
  3. scripts to run OpenBooks in on your local database installation.

Follow the simple instructions below to build and run OpenBooks:
  1. Download OpenBooks distribution.
  2. Configure build and run environment.
  3. Build OpenBooks from source
  4. Run OpenBooks

Download Instructions

OpenBooks is distributed under Apache License, Version 2.0
OpenBooks can be checked out from OpenJPA repository.
  $ svn co https://svn.apache.org/repos/asf/openjpa/trunk/openjpa-examples/openbooks.
will check out the source code and build scripts of OpenBooks in current directory.
OpenBooks requires following software environment to run:
  1. Java Runtime version 6.0 or higher
  2. OpenJPA Libraries version 2.0 or higher
  3. Ant version 1.6 or higher
  4. Any JDBC complaint database supported by OpenJPA. See OpenJPA documentation for a list of supported databases.

Configure build and run environment

OpenBooks builds with Ant. The Ant build script is somewhat involved because OpenBooks can be built and packaged either as a JSE (Swing based) application or a JEE Web Application. By default, OpenBooks is built as a JSE application.
Before you run a build, configure the build environment by editing build.properties. Essentially, you need to
  1. point openjpa.lib variable to the local directory where OpenJPA class library(ies) reside. Notice that the variable points to a directory and not a *.jar file. All *.jar files found under the directory are included in compilation classpath.

The next step is to configure runtime environment.
  1. Edit persistence.xml located in resources/META-INF directory. Modify the javax.persistence.jdbc.driver and javax.persistence.jdbc.url property to suit your local database and its driver.
    The example persistence.xml provided is primarily for a JSE environment. In JEE environment, you may already have a JTA data source configured and registered in JNDI. Of course, then the persistence.xml is to be edited accordingly in <jta-data-source> clause.
  2. In JSE mode, edit run.properties to specify location of OpenJPA class libraries and JDBC Driver used in runtime classpath.
    In JEE mode, OpenJPA library and JDBC drivers are configured in JEE server and hence variables in this file are irrelevant.
Both build.properties and run.properties files are commented in-place on what is to be edited.

Build OpenBooks from source

Once you have configured the environment, simply issue
  $ ant
or
  $ ant -Dbuild.mode=jee
The default target of the ant script will
  • generate metamodel classes (required for Criteria API)
  • compile the source code
  • enhance the persistence domain model
  • package the application based on the build.mode as a Swing-based application or a Web Application Archive. and
  • copy the deployable artifacts to target/openbooks directory relative to the current directory.

    Run OpenBooks

    If you have built OpenBooks for JSE, then go to the target/openbooks directory.
    Invoke the Ant script to run OpenBooks
      $ ant -f run.xml
    If you have build OpenBooks for JEE, a Web Application Archive openbooks.war will be created in target/openbooks directory. You need to deploy openbooks.war to a JEE Application Server. Once deployed, you can point a browser to Application Server URL
      http://<app server host>:<port>/openbooks/
    to access OpenBooks as an web application.

    Populate OpenBooks Database

    OpenBooks checks for existing data at first connection to the database. If the database is empty, the schema is defined and populated with initial data. However, you can explicitly populate the database in JSE build. Edit load.properties to specify load parameters such as number of Books etc. OpenBooks uses this data to populate a database with some sample data. This example file has some typical values. If you are satisfied with it, you can leave them as it is. Then invoke the Ant script
      $ ant -f run.xml load