View Javadoc
1   package ejava.examples.orm.inheritance.annotated;
2   
3   import javax.persistence.*;
4   
5   /**
6    * This class provides a base class for table-per-class inheritance strategy
7    * example. The derived classes will squash the information from this entity
8    * within their tables.<p/>
9    * 
10   * Note too that since there is no physical base table, something external 
11   * must be used to create id values or allow the ids of each sub-type to 
12   * be allowed to be locally generated (thus getting overlaps in id values).
13   * The approach taken here was to define a sequence generator.
14   */
15  @Entity
16  @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
17  @SequenceGenerator(
18          name="orminhSeq", //required logical name
19          sequenceName="ORMINH_SEQ" //name in database
20  )
21  public abstract class Account {
22      @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="orminhSeq")
23      private long id;
24      private double balance;
25      
26      public Account() {}
27      public Account(long id) { this.id=id; }
28      public long getId() { return id; }
29      
30      public double getBalance() { return balance; }
31      public void setBalance(double balance) {
32          this.balance = balance;
33      }
34      
35      public void deposit(double amount) throws AccountException {
36          setBalance(getBalance() + amount);
37      }
38      public abstract void withdraw(double amount) throws AccountException;
39      public void processInterest() {}
40      
41      public String toString() {
42          StringBuilder text = new StringBuilder(super.toString());
43          text.append(", id=" + id);
44          text.append(", balance=" + balance);
45          return text.toString();
46      }
47  }