View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import javax.persistence.*;
7   
8   /** 
9    * This class provides an example of using the java.util.Map with 
10   * relationships.
11   */
12  @Entity @Table(name="ORMREL_LIBRARY")
13  public class Library {
14      @Id @GeneratedValue
15      private long id;
16      @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
17      @MapKey(name="id")
18      private Map<Long, Borrower> borrowers = new HashMap<Long, Borrower>();
19      
20      public Library() {}
21      public Library(long id) { this.id = id; }
22  
23      public long getId() { return id; }
24      
25      public Map<Long, Borrower> getBorrowers() { return borrowers; }
26      public void setBorrowers(Map<Long, Borrower> borrowers) {
27          this.borrowers = borrowers;
28      }
29  
30      private String myInstance() {
31          String s=super.toString();
32          s = s.substring(s.lastIndexOf('.')+1);
33          return s;
34      }
35  
36      public String toString() {
37          StringBuilder text = new StringBuilder(myInstance());
38          text.append(", id=" + id);
39          text.append(", borrowers(" + borrowers.size() + ")={");
40          for(long bid: borrowers.keySet()) {
41              text.append("id=" + bid + "{" +
42                      borrowers.get(bid).getName() +
43                      "}, ");
44          }
45          text.append("}");
46          return text.toString();
47      }
48  
49  }