View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   
6   import javax.persistence.*;
7   
8   /**
9    * This class represents a media topic, for which there are many copies and
10   * authors. There are also many media in an Inventory<p/>
11   * 
12   * It is an example of a ManyToOne uni-directional relationship. There are
13   * zero to many MediaCopy(s) for each Media. However, there is no reference to 
14   * the copies from Media. All access to the copies must come through querying
15   * the MediaCopies. MediaCopy happens to both "own" the relationship and 
16   * be the only that it can be navigated by (uni-directional).<p/>
17   * 
18   * It is also an example of a OneToMany uni-directional relationship using 
19   * a Join (or Link) table. There are zero to many Media in Inventory. However, 
20   * there is no reference to the Inventory from this class or MEDIA table. The
21   * linkage is done through a separate table defined in the Inventory class.
22   * Inventory both "owns" the relationship and is the only side that it can
23   * be navigated by.
24   * 
25   * It is also an example of ManyToMany bi-directional relationship. Each Media
26   * can have zero to many Authors. Each Author can have zero to many Media.
27   * Author "owns" the relationship (thus defines the join/link table). Media
28   * only defines the mapping back to Author. The relationship can be navigated
29   * by either side.
30   */
31  @Entity @Table(name="ORMREL_MEDIA")
32  public class Media  {
33      //private static Log logger = LogFactory.getLog(Media.class);
34  
35      @Id @GeneratedValue @Column(name="MEDIA_ID")
36      private long id;
37      private String title;    
38      @ManyToMany(mappedBy="media")  //names property in Author that points to us
39      private Collection<Author> authors = new ArrayList<Author>();
40      
41      public Media() { 
42          //logger.debug(super.toString() + ": ctor()"); 
43      }
44  
45      public long getId() { return id; }
46      
47      public String getTitle() { return title; }
48      public void setTitle(String title) {
49          this.title = title;
50      }        
51  
52      public Collection<Author> getAuthors() { return authors; }
53      public void setAuthors(Collection<Author> authors) {
54          this.authors = authors;
55      }
56      
57      private String myInstance() {
58          String s=super.toString();
59          s = s.substring(s.lastIndexOf('.')+1);
60          return s;
61      }
62  
63      public String toString() {
64          StringBuilder text = new StringBuilder(myInstance());
65          text.append(", id=" + id);
66          text.append(", title=" + title);
67          text.append(", authors(" + authors.size() + ")={");
68          for (Author a: authors) {
69              text.append(a.getId() + ", ");
70          }
71          text.append("}");
72          return text.toString();
73      }
74  }