Writing a Session Bean facade

Now your Entity Bean is ready, we'll concentrate on the Session Bean which will be our facade for our Entities.

A session bean facade can be used to encapsulate the complexity of interactions between the business objects participating in a workflow. The Session Facade manages the business objects, and provides a uniform coarse-grained service access layer to clients.


import java.io.Serializable;

import java.util.List;

import javax.ejb.*;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.annotation.ejb.Clustered;


public class HelloBean implements 
                          HelloBeanItf, Serializable {

private EntityManager em;

 public List<Note> findAll() {
  Query query = em.createQuery("Select h from Note h");
  return (List<Note>) query.getResultList();


That's all, besides the Stateless annotation we meet the EntityManager beast ! what is it ?

The Entity Manager is responsible for the entity objects and handling their persistence. The entity manager itself can handle multiple entity instances, the set of which is referred to as a Persistence context. For each record in the database, there will be only one instance of that entity bean stored inside the persistence context.

EntityManager itself comes from factories defined by the EntityManagerFactory interface.
The factory can then create server persistence units. In fact, different factories can reference the same persistence context. We are able to create an EntityManager from the factory via container-managed injection by @PersistenceContext. The parameter unitName contains the equivalent

<persistence-unit name="userDatabase"> specified in the persistence.xml  file.

Finally, the last type of manipulation you are going to want to do is to find objects by queries. If you are familiar with the Hibernate way of using Query objects to query the database, this will come fairly easy to you.

public List<Note> findAll() {
  Query query = em.createQuery("Select h from Note h");
  return (List<Note>) query.getResultList();

In the previous code, we are finding all objects mapped as Note and returning them in a plain java.util.List collection. The language used to query the object is similar to SQL but it's not regular SQL, it's called Java Persistence Query Language. This language gives you a way to specify the semantics of queries in a portable way, independent of the particular database you're using in an enterprise environment. We suggest you for further reading about the JPQL these tutorials:



Packaging the EJB Entity application

Packaging the application simply requires that you "jar" your classes (and the persistence.xml file) and copy the file to the "deploy" folder. Here's how the Entity bean application looks like:

jboss ejb 3 entity
Picture 1: the Entity Bean application "exploded"

Let's improve our example: how to insert an object ?

quite simple, create the Object as a POJO and pass it to the Entity Manager which will care about persisting it !

Add this to your SLSB:

public void newNote(Note note) {
try {
catch (Exception e) {

Then in your Client:

Note newnote = new Note();
newnote.setAttachment("text attached");
newnote.setText("full text");


Ok but what about deleting an Object ?

Deleting an Object (and therefore a row) from your SLSB requires finding it first and then calling the remove method on the Entity Manager.

public void deleteNote(long noteId) {
  Note note = em.find(Note.class, noteId);

Ok but what about updating an Object ?

If you have understood how the EM works you shouldn't even ask it! anyway it works the same as for Deleting an Object: find it first and change the property you like.

public void deleteNote(long noteId) {
  Note note = em.find(Note.class, noteId);
  note.setText("update the text please!");

Which IDE can I use to develop my Entity Beans ?

Without the burden of configuration files you can use any IDE you like. For example here's how the project would like in a vanilla Eclipse Java Project. You just need to add the libraries which are necessary for Annotations and data persistence. These libraries can be found in the client directory of JBoss installation. 

jboss ejb 3 entity
Picture 2: Eclipse package explorer with the libraries needed to compile the project

In this article we've seen how to create a simple Entity Bean which can be deployed to JBoss Server in a matter of minutes. If you missed the article about Session Bean 3.0 read it in the JBoss application server section.