The roster application demonstrates how to use entity inheritance, as described in Entity Inheritance.
The League entity in roster is an abstract entity with two concrete subclasses: SummerLeague and WinterLeague. Because League is an abstract class it cannot be instantiated:
... @Entity @Table(name = "EJB_ROSTER_LEAGUE") public abstract class League implements java.io.Serializable { ... }
Instead, SummerLeague or WinterLeague are used by clients when creating a league. SummerLeague and WinterLeague inherit the persistent properties defined in League, and only add a constructor that verifies that the sport parameter matches the type of sport allowed in that seasonal league. For example, here is the SummerLeague entity:
... @Entity public class SummerLeague extends League implements java.io.Serializable { /** Creates a new instance of SummerLeague */ public SummerLeague() { } public SummerLeague(String id, String name, String sport) throws IncorrectSportException { this.id = id; this.name = name; if (sport.equalsIgnoreCase("swimming") || sport.equalsIgnoreCase("soccer") || sport.equalsIgnoreCase("basketball") || sport.equalsIgnoreCase("baseball")) { this.sport = sport; } else { throw new IncorrectSportException( "Sport is not a summer sport."); } } }
The roster application uses the default mapping strategy of InheritanceType.SINGLE_TABLE, so the @Inheritance annotation is not required. If you wanted to use a different mapping strategy, decorate League with @Inheritance and specify the mapping strategy in the strategy element:
@Entity @Inheritance(strategy=JOINED) @Table(name="EJB_ROSTER_LEAGUE") public abstract class League implements java.io.Serializable { ... }
roster uses the default discriminator column name, so the @DiscriminatorColumn annotation is not required. Because you are using automatic table generation in roster the Persistence provider will create a discriminator column in the EJB_ROSTER_LEAGUE table called DTYPE, which will store the name of the inherited entity used to create the league. If you want to use a different name for the discriminator column, decorate League with @DiscriminatorColumn and set the name element:
@Entity @DiscriminatorColumn(name="DISCRIMINATOR") @Table(name="EJB_ROSTER_LEAGUE") public abstract class League implements java.io.Serializable { ... }