Skip to content

EJBs in the three tier system

This is primarily written as a basic primer for those CMP students on the same course as me that are struggling and or frustrated with Enterprise JavaBeans. I won’t give any course work specific info, but hopefully this may help at least a bit.

Firstly I should probably mention I have worked in a real software development environment coding EJBs and I’m also using the technology in my third year project, so there is a real practical use for it in the big bad world.

So, what the heck is all this EJB stuff?
Well Enterprise JavaBeans allow you, amongst other things, to get data from a database and manipulate it. Using EJBs adds an extra tier to your system architecture, which essentially cuts down on the amount of SQL and database connectivity code you have to write (see here for more info about web application architecture). You’ve got three main types of beans:

  • Entity beans – these represent tables in the database. They’re just a class with a few annotations which tell the EJB container to map between the table columns and the fields in the class.
  • Example:
    public class EntityName implements Serializable {
    private Integer id; //column 1
    private String name; //column 2
    private List<AnotherEntity> otherEntities = new ArrayList<AnotherEntity>(); //a one to many relationship
    public EntityName () { }
    @GeneratedValue(strategy=GenerationType.IDENTITY) //container generates id
    @Column(name=”id”) //here we tell the container the name of the column this field maps to
    public Integer getId() {
    return id;
    public void setId(Integer id) { = id;
    public String getName() {
    return name;
    public void setName(String name) { = name;
    public List<AnotherEntity> getOtherEntities() {
    return otherEntities;
    public void setOtherEntities(List<AnotherEntities> entities) {
    this.otherEntities = entities;

  • Session Beans – this is where you stick all your actual code which does stuff with the entities.
  • Example:
  • @Stateless
  • public class DoStuffSessionBean implements DoStuffInterface {
  • @PersistenceContext(unitName=”PERSISTENCE_CONTEXT”) //defines where mapping to database
  • private EntityManager entityManager; //entity manager allows you to insert, update and delete tables in the database
  • @Override
  • public AnotherEntity findAnotherEntityById(Integer id) {
  • try {
  • return entityManager.find(Location.class, id);
  • } catch(NoResultException e) {
  • return null;
  • }
  • }
    … (thats a pretty boring session bean, see entityManager.merge, entityManager.persist and entityManager.remove for more things you can do
  • Message Driven Beans, these are nothing to do with the database but are very useful for delivering messages for event driven processes, not really covered in the module we are carrying out though

Great, but how do I set it all up in the first place?
That’s half the battle with this sort of stuff. I use Eclipse IDE, you can create a new enterprise application which gives a nice structure to your project, NetBeans will also do this for you though. Its worth noting our lab machines have no facilities for developing EJB components as NetBeans doesn’t link to any EJB compatible server (e.g. JBOSS or Glassfish, it does have TomCat but this is only a servlet container so can’t do it), and the version of Eclipse is not Enterprise Edition.
So its down to using your own machines for development and then deploying on JBOSS when you’re done, or fiddle with the lab machines a bit… Eclipse runs out of a directory (of course you would ask a member of staff first before doing this in case we are not supposed to do this…)

OK what’s the point in doing it this way?
Aside from the benefits already mentioned, EJBs are very scalable due to session beans. Instead of opening and closing database connections specifically, the EJB container manages all this for you and selects a bean out of a pool of them which are ready and waiting to process the method you want to perform. Basically when you have thousands of users of a system, all heavily hitting the database it enables your web application to still respond quickly.

How does it fit in with the rest of the application?
Well, you use your session bean to gain access to, create and modify your entity beans. You can access the session bean from a servlet (using a JNDI lookup or dependancy injection – a simple @EJB annotation). There is some setup to do in the way of XML files, but IDEs can get you a long way in that respect.

Anything else?
There are loads of great tutorials but be aware there are a shed load of different ways to implement EJBs, so make sure at the very least its an example for EJB 3.0 you’re looking at. If something isn’t working out from one tutorial, take a look at some others and see what the most commonly used approaches are. 

I hope that very short intro is in some part useful to people. I have very little time to put into something like this, but I know there are going to be people tearing their hair out (to put it mildly) with this particular course work.

One Comment

  1. Thanks says:


Leave a Reply