View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import javax.persistence.*;
4   
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import ejava.examples.orm.rel.MediaCopyPK2;
9   
10  /**
11   * This version of MediaCopy uses the @MapsId annotation to link the 
12   * relationship and primary key properties together. This technique 
13   * was added in JPA 2.0 and is stated to be preferred over the JPA 1.0
14   * indirect technique.
15   */
16  
17  @Entity @Table(name="ORMREL_MEDIACOPY2")
18  @IdClass(MediaCopyPK2.class)
19  @AttributeOverrides({
20  	@AttributeOverride(name="copyNo", column=@Column(name="COPY_NO"))
21  })
22  public class MediaCopy2 {
23      private static final Logger log = LoggerFactory.getLogger(MediaCopy2.class);
24      @Id //mapped to COPY_NO by attribute override
25      private int copyNo;    
26      @Id 
27      @ManyToOne 
28      @JoinColumn(name="MEDIACOPY_MID")
29      private Media media;
30      
31      protected MediaCopy2() { log.debug(super.toString() + ": ctor()"); }
32      public MediaCopy2(Media media, int copyNo) {
33          log.debug(super.toString() + ": ctor() mediaId="
34                  + media.getId() + ", copyNo=" + copyNo);
35          setMedia(media);
36          setCopyNo(copyNo);
37      }    
38  
39      public int getCopyNo()                { return copyNo; }    
40      private void setCopyNo(int copyNo)    { this.copyNo = copyNo; }
41      
42      public long getMediaId()              { return media.getId();} //mediaId; }
43      
44      public Media getMedia()               { return media; }    
45      private void setMedia(Media media)    { this.media = media; }
46  
47      private String myInstance() {
48          String s=super.toString();
49          s = s.substring(s.lastIndexOf('.')+1);
50          return s;
51      }
52  
53      public String toString() {
54          return myInstance() +
55              ", mediaId=" + getMediaId() +
56              ", copyNo=" + getCopyNo() +
57              ", media=" + getMedia();
58      }
59  }