The tutorial example uses the single table strategy to map an inheritance relationship of Pet, which is the base class for Cat and Dog.
With the single table strategy, the entire class hierarchy is persisted in one big single table. A discriminator column is required to differentiate between which class type is persisted in a particular row.
This is what the annotations look like for Pet.
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "ANIMAL_TYPE", discriminatorType = DiscriminatorType.STRING) public class Pet implements java.io.Serializable {
The @DiscriminatorColumn specifies the column that will hold the type of the persisted entity. For subclasses, they must define the value of the discriminator column that will identify the class.
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING) @DiscriminatorValue("DOG") public class Dog extends Pet {
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.
create table PET ( ID integer primary key, ANIMAL_TYPE varchar, NAME varchar, WEIGHT double, LIVES int, NUMBONES int );
Unix: $ export JBOSS_HOME=<where your jboss 4.0 distribution is> Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is> $ ant $ ant run run: [java] 2004-10-07 00:16:20,395 INFO org.jboss.remoting.InvokerRegistry[main] - Failed to load soap remoting transpo rt: org/apache/axis/AxisFault [java] Sox [java] Junior
The INFO message you can ignore. It will be fixed in later releases of JBoss 4.0.