Developing a JPA application on JBoss AS 7

19 Jul, 2011

In my previous post we setup a Maven/Eclipse project for developing RESTful web applications on JBoss AS 7. A RESTful web service that is not using a database is some what of an oddity. Therefor in this blog we’ll extend the project with JPA.

Configuration: Adding a JBoss Module

I want to use Postgres as the underlying database. To install the required JDBC driver for Postgres in the JBoss AS 7 container, we need to add a module. As I mentioned in the previous post, the module structure of JBoss is reflected on the file system.

The files in the ”main” directory that constitute the module are the folloing:

  • module.xml describes the module. The content follows below.
  • postgresql-9.0-801.jdbc4.jar is the postgres JDBC4 driver.
  • postgresql-9.0-801.jdbc4.jar.index is an index file generated by JBoss Annotation Indexer when the modules are scanned.

So in order to add a module to the JBoss container, we need to write an XML file:
[xml title=”module.xml”]
<module xmlns="urn:jboss:module:1.0" name="org.postgres">
<resource-root path="postgresql-9.0-801.jdbc4.jar"/>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
Now we also need to enlist the datasource in the standalone.xml: the main configuration for JBoss. You’ll find it in the ”standalone/configuration” subdirectory of the JBoss installation. The file already has a datasource and driver for h2. So we add a new node:
[xml title=”standalone.xml” highlight=”9,10-13″]
<?xml version=’1.0′ encoding=’UTF-8′?>
<server name="brandhout.local" xmlns="urn:jboss:domain:1.0">


<driver name="postgres" module="org.postgres">
<driver name="h2" module="com.h2database.h2">

Now, after restarting the server, we can finish the configuration using the admin console.

The ”New Datasource” button will open up a dialog that will guide you through a few simple steps to configure the datasource.
The changes will be persisted in the standalone.xml, which will now contain a fragment like this:
[xml title=”standalone.xml”]
<datasource jndi-name="LibraryDS" pool-name="LibraryDS_Pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">


Reading from the database

Now we need to configure our application to read from the database. To configure the database as a persistence context in JPA, we add a persistence.xml to our project.
[xml language=”src/main/resources/persistene”]
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="" xmlns:xsi=""
<persistence-unit name="library" transaction-type="JTA">
<property name="" value="create-drop" />
The java code we have to write is quite trivial JPA code mixed in with some JAX-RS annotation. For completeness they are included here below (collapsed).
[java title=”com/xebia/library/model/” collapse=”true”]
package com.xebia.library.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@SequenceGenerator(name="BOOK_SEQ", sequenceName="BOOK_SEQ")
public class Book {
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BOOK_SEQ")
private Long id;
private String title;
public Long getId() {
return id;
public String getTitle() {
return title;
[java title=”com/xebia/library/” collapse=”true”]
package com.xebia.library;
import static;
import java.util.List;
import javax.ejb.Local;
import org.jboss.resteasy.annotations.Form;
import com.xebia.library.model.Book;
public interface BookRepository {
List<Book> all();
Book create(@Form Book entity);
Book getById(@PathParam("id") long id);
Book update(@Form Book entity);
void remove(@PathParam("id") long id);
[java title=”com/xebia/library/impl/” collapse=”true”]
package com.xebia.library.impl;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.xebia.library.BookRepository;
import com.xebia.library.model.Book;
public class BookRepositoryBean implements BookRepository {
private EntityManager em;
public List<Book> all() {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Book> query = builder.createQuery(Book.class);
Root<Book> root = query.from(Book.class);
return em.createQuery(query).getResultList();
public Book create(Book book) {
return book;
public Book byId(long id) {
return em.find(Book.class, id);
public Book update(long id, Book book) {
return em.merge(book);
public void delete(long id) {
A few things to notice:

  • The JPA entity is also used as a ”backing” object for Form posts. The @FormParam annotation is used to parameters posted to the fields of the object.
  • The Repository is now also used as a local interface for the stateless bean.

After deploying the application to the server, we can test it using this simple tool.


With just a few classes we were able to deploy a RESTful web service that enables users to maintain a (rudimentary) book list. The WAR that we build is really tiny: It only contains a few classes. The application relies on JEE specifications that are implemented in the JBoss container. A few remarks about this:

  1. Developing a JEE application becomes much simpler and more fun! It feels almost like RAD web development alla Grails.
  2. Testing an application like this is non-trivial: Much of the functionality is provided by the container. With a ‘thicker’ maven project with Hibernate and Spring/Seam as dependencies, we could mimic (parts of) the deployed application in an in-build testing environment. How do we do this now? One possible answer is to use Arquillian. A future blog will have to cover that…

Of course the application we have now is far from finished. To start with, it has no user interface…

Explore related posts