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…

Newest Most Voted
Inline Feedbacks
View all comments
10 years ago

Great article. Very useful, thanks.
One thing might worth mentioning is that one should do the postgre configuration, e.g. creating /postgres/main and the files in it on a stopped server, because if you are doing it on a running instance of JBoss 7, the folders are going to be deleted.

Frédéric Chopin
Frédéric Chopin
10 years ago

Thank you!… I change “postgres” for “postgresql” for not confusion.

10 years ago

Dag Maarten,
Interessant artikel. Ik heb een vraag en kan het antwoord niet vinden in diverse Hibernate en JBoss groepen. Misschien heb jij een tip?
Ik ben met een migratie van JBoss 4.2 naar JBoss 7.0.2 bezig en heb mijn oorspronkelijke opzet van een EAR met een WAR en EJB-JAR gewijzigd in een WAR met alleen een EJB-JAR.
Ik krijg nu, als ik vanuit een servlet de entitymanager aanroep en een JPA “find” doe op een Entity die in de EJB-JAR zit, de volgende exception:
“org.hibernate.HibernateException: proxy handle is no longer valid”.
De Entity die ik aanroep, linkt naar andere Entities maar ik doe niets anders dan de EJB laden en doe geen query op gelinke entities. Heb jij dit wellicht bij de hand gehad?
Alvast bedankt,

Charles Moulliard
10 years ago

Hi Maarteen,
This is an really and great blog that you have published about JPA on JBoss AS7. Just a quick question, have you published your project in a GitHub repo ? Have you added a beans.xml file to allow the CDI container to inject dependencies ? I do not see in your explanations how the persistence manager is injected into the BookRepositoryBean class ? Is it done automatically by JBoss AS7 or do we have to install something for EJB 3.0 spec ?
Charles M.

Sandy Noa Cabrera
9 years ago

Hi, what about the JBossAS7 configuration for Oracle ?

9 years ago

I tried it but something is still wrong,
i keep getting this error:
java.lang.NoClassDefFoundError: org/postgresql/copy/CopyManager
any idea?
Anyway, thank for the contribution

Explore related posts