JBoss.org Community Documentation

Chapter 19. Introduction Join Inheritance in EJB3 Entities

The EJB3 specification allows you to define entities that inherit from one another. The inheritance relationships can be reflected in queries as well. So, if you queried based on the base class, the query is polymorphic.

The tutorial example uses the join table strategy to map an inheritance relationship of org.jboss.tutorial.joininheritance.bean.Pet, which is the base class for org.jboss.tutorial.joininheritance.bean.Cat and org.jboss.tutorial.joininheritance.bean.Dog.

With the join table strategy there is a table per class in the hierarchy, but the subclass tables only have the extra attribute they define in their subclass. A discriminator column is NOT required to differentiate between which class type is persisted in a particular row unlike the single table mapping. The persistence manager does not need a discrimiator column to figure out the type.

This is what the annotations look like for Pet:

@Inheritance(strategy = InheritanceType.JOINED)
public class Pet implements java.io.Serializable


@Inheritance(strategy = InheritanceType.JOINED)
public class Dog extends Pet


Polymorphic Queries :

org.jboss.tutorial.joininheritance.bean.PetDAOBean stateless EJB wraps some polymorphic queries.

public List findByWeight(double weight)
   return manager.createQuery("from Pet p where p.weight < :weight").setParameter("weight", weight).getResultList();


Even though the findByWeight method queries on Pet, either Dog or Cat instances can be returned.

Table Mapping :

The table mapping for this example looks like this:

create table PET (
  ID integer primary key,
  ANIMAL_TYPE varchar,
  NAME varchar,
  WEIGHT double

create table CAT (
  ID integer primary key,
  LIVES int

create table DOG (
  ID integer primary key,



The join inheritance mapping is less efficient than the single table strategy as the SQL query is more complicated.

Building and Running

From the command prompt, move to the "joininheritance" folder under the Section 1.3, “Set the EJB3_TUTORIAL_HOME”

Ant Users:

Make sure your JBossAS-5.x is running

$ ant
$ ant run

     [java] Sox
     [java] Junior


Maven Users: Make sure the AS is not running.

$ mvn clean install -PRunSingleTutorial

View the tables and rows:

You can view the tables created by JBoss by going to the Hypersonic Service, scrolling down to the startDatabaseManager button and clicking it. A Hypersonic SQL window will be minimized, but you can open it up to look at the tables and do queries.