1 package ejava.examples.orm.onetomany.annotated;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import javax.persistence.*;
7
8 /**
9 * This class implements a parent in a OneToMany, uni-directional relationship
10 * test case. We are looking to see where the foreign key gets placed; in
11 * a link/join table or the child side of the relationship. The child class
12 * knows nothing of this relationship.<p/>
13 *
14 * This is the schema when nothing besides @OneToMany is defined<p/>
15 * <pre>
16 @OneToMany
17 public Collection<OneManyChild> getChildren() {
18 return children;
19 }
20 </pre><p/>
21 * <pre>
22 create table ORMO2M_CHILD (
23 CHILDID bigint generated by default as identity (start with 1),
24 name varchar(255),
25 primary key (CHILDID)
26 );
27
28 create table ORMO2M_PARENT (
29 PARENTID bigint generated by default as identity (start with 1),
30 name varchar(255),
31 primary key (PARENTID)
32 );
33
34 create table ORMO2M_PARENT_ORMO2M_CHILD (
35 ORMO2M_PARENT_PARENTID bigint not null,
36 children_CHILDID bigint not null,
37 unique (children_CHILDID)
38 );
39 alter table ORMO2M_PARENT_ORMO2M_CHILD
40 add constraint FK9732C673B120B8B4
41 foreign key (children_CHILDID)
42 references ORMO2M_CHILD;
43
44 alter table ORMO2M_PARENT_ORMO2M_CHILD
45 add constraint FK9732C673C25EC1A0
46 foreign key (ORMO2M_PARENT_PARENTID)
47 references ORMO2M_PARENT;
48
49 </pre><p/>
50 *
51 * Adding @JoinColumn(name="PARENT_ID") to the parent side...
52 * <pre>
53 @OneToMany
54 @JoinColumn(name="PARENT_ID")
55 public Collection<OneManyChild> getChildren() {
56 return children;
57 }
58 </pre><p/>
59 * <pre>
60 create table ORMO2M_CHILD (
61 CHILDID bigint generated by default as identity (start with 1),
62 name varchar(255),
63 PARENT_ID bigint,
64 primary key (CHILDID)
65 );
66
67 create table ORMO2M_PARENT (
68 PARENTID bigint generated by default as identity (start with 1),
69 name varchar(255),
70 primary key (PARENTID)
71 );
72 alter table ORMO2M_CHILD
73 add constraint FK257187DDF37CA975
74 foreign key (PARENT_ID)
75 references ORMO2M_PARENT;
76 </pre><p/>
77 *
78 * Adding @JoinTable/@JoinColumn(name="PARENT_ID") to the parent side...
79 * <pre>
80 @OneToMany
81 @JoinTable(
82 joinColumns=@JoinColumn(name="PARENT_ID"))
83 public Collection<OneManyChild> getChildren() {
84 return children;
85 }
86 </pre><p/>
87 * <pre>
88 create table ORMO2M_CHILD (
89 CHILDID bigint generated by default as identity (start with 1),
90 name varchar(255),
91 primary key (CHILDID)
92 );
93
94 create table ORMO2M_PARENT (
95 PARENTID bigint generated by default as identity (start with 1),
96 name varchar(255),
97 primary key (PARENTID)
98 );
99
100 create table ORMO2M_PARENT_ORMO2M_CHILD (
101 PARENT_ID bigint not null,
102 children_CHILDID bigint not null,
103 unique (children_CHILDID)
104 );
105 alter table ORMO2M_PARENT_ORMO2M_CHILD
106 add constraint FK9732C673B120B8B4
107 foreign key (children_CHILDID)
108 references ORMO2M_CHILD;
109
110 alter table ORMO2M_PARENT_ORMO2M_CHILD
111 add constraint FK9732C673F37CA975
112 foreign key (PARENT_ID)
113 references ORMO2M_PARENT;
114 </pre><p/>
115 *
116 *
117 */
118 @Entity(name="O2MOwningParent") @Table(name="ORMO2M_PARENT")
119 public class OneManyOwningParent {
120 @Id @GeneratedValue @Column(name="PARENTID")
121 private long id;
122
123 private String name;
124
125 @OneToMany
126 @JoinColumn(name="PARENT_ID")
127 //@JoinTable(
128 // joinColumns=@JoinColumn(name="PARENT_ID"))
129 private Collection<OneManyChild> children = new ArrayList<OneManyChild>();
130
131 public OneManyOwningParent() {}
132 public OneManyOwningParent(long id) { this.id = id; }
133 public OneManyOwningParent(String name) { this.name = name; }
134
135 public long getId() { return id; }
136
137 public String getName() { return name; }
138 public void setName(String name) {
139 this.name = name;
140 }
141
142 public Collection<OneManyChild> getChildren() { return children; }
143 public void setChildren(Collection<OneManyChild> children) {
144 this.children = children;
145 }
146
147 public String toString() {
148 StringBuilder text = new StringBuilder();
149 text.append("id=" + id);
150 text.append(", name=" + name);
151 text.append(", children=(" + children.size() + ")" + children);
152 return text.toString();
153 }
154 }