View Javadoc
1   package ejava.examples.orm.inheritance.annotated;
2   
3   import javax.persistence.*;
4   
5   /**
6    * This class provides an example base class in a single table inheritance
7    * strategy. In this case, a single table is defined to hold the properties of
8    * all sub-classes. A discriminator field is necessary to denote the type
9    * of each row in the table. Note that the primary key is being generated 
10   * here since this class definition is shared among the sub-classes.
11   */
12  @Entity @Table(name="ORMINH_PRODUCT")
13  @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
14  @DiscriminatorColumn(name="PTYPE", //column in root table indicating type
15      discriminatorType=DiscriminatorType.STRING,//data type of column
16      length=32) //length of discriminator string
17  public abstract class Product {
18      @Id @GeneratedValue
19      private long id;
20      private double cost;
21      
22      protected Product() {}
23      protected Product(long id) { this.id=id; }
24      public long getId() { return id; }
25  
26      public double getCost() {
27          return cost;
28      }
29      public void setCost(double cost) {
30          this.cost = cost;
31      }
32      
33      @Transient
34      public abstract String getName();
35      
36      public String toString() {
37          StringBuilder text = new StringBuilder(super.toString());
38          text.append(", id=" + id);
39          text.append(", cost=" + cost);
40          return text.toString();
41      }
42  }