View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.persistence.*;
7   
8   /**
9    * This class provides an example of one end of a ManyToMany relationship. 
10   * This class will "own" the relationship, thus define the join table.
11   */
12  @Entity @Table(name="ORMREL_AUTHOR")
13  public class Author {
14      @Id @GeneratedValue
15      private long id;
16      private String name;
17  
18      @ManyToMany
19      @JoinTable(name="ORMREL_AUTHOR_MEDIA", //defines the link table
20                  //defines the column in the link table for author FK
21              joinColumns={@JoinColumn(name="LINK_AUTHOR_ID")},
22                  //defines the column in the link table for the media FK
23              inverseJoinColumns={@JoinColumn(name="LINK_MEDIA_ID")})
24      @OrderBy("title DESC") //order the list returned from database
25      private List<Media> media = new ArrayList<Media>();
26      
27      public Author()        {}
28      public Author(long id) { this.id = id; }
29      
30      public long getId() { return id; }    
31      
32      public List<Media> getMedia() { return media; }
33      public void setMedia(List<Media> media) {
34          this.media = media;
35      }
36  
37      public String getName() { return name; }
38      public void setName(String name) {
39          this.name = name;
40      }
41      
42      private String myInstance() {
43          String s=super.toString();
44          s = s.substring(s.lastIndexOf('.')+1);
45          return s;
46      }
47      
48      public String toString() {
49          StringBuilder text = new StringBuilder(myInstance());
50          text.append(", id=" + id);
51          text.append(", name=" + name);
52          text.append(", media(" + media.size() + ")={");
53          for (Media m: media) {
54              text.append(m.getId() + ", ");
55          }
56          text.append("}");
57          return text.toString();
58      }
59  }