View Javadoc
1   package ejava.examples.orm.map.annotated;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import javax.persistence.*;
7   
8   /**
9    * This class represents a parent in a One-to-Many, uni-directional
10   * relationship, relative to the parent. The OneManyChild
11   * makes no reference to this relationship. Of note, there is no such
12   * thing as a One-to-Many, bi-directional relationship, owned by the parent 
13   * in JPA. All One-to-Many bi-directional relationships must be owned by
14   * the child.
15   */
16  @Entity @Table(name="ORMMAP_ONEMANY_OWNPARENT")
17  public class OneManyOwningParent {
18      @Id @Column(name="ID")
19      private String name;
20  
21      @ManyToMany
22      @MapKey(name="name")
23      @JoinTable(name="ORMMAP_OWNPARENT_INVCHILD_LINK",
24          joinColumns=@JoinColumn(name="PARENT_ID"),
25          inverseJoinColumns=@JoinColumn(name="CHILD_ID"))
26      private Map<String, OneManyChild> ownedChildren =
27              new HashMap<String, OneManyChild>();
28      
29      protected OneManyOwningParent() {}
30      public OneManyOwningParent(String name) { this.name = name; }
31      
32      public String getName() { return name; }
33      public void setName(String name) {
34              this.name = name;
35      }
36      
37      public Map<String, OneManyChild> getOwnedChildren() { return ownedChildren; }
38      public void setOwnedChildren(Map<String, OneManyChild> ownedChildren) {
39              this.ownedChildren = ownedChildren;
40      }
41      
42      public String toString() {
43          StringBuilder text = new StringBuilder();
44          text.append(getClass().getName());
45          text.append(", name=" + name);
46          if (ownedChildren != null) {
47                  text.append(", ownedChildren=" + ownedChildren);
48          }		
49          return text.toString();
50      }
51  }