KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cfg > Ejb3JoinColumn


1 //$Id: Ejb3JoinColumn.java,v 1.22 2005/07/22 02:33:21 epbernard Exp $
2
package org.hibernate.cfg;
3
4 import java.util.Map JavaDoc;
5 import java.util.Set JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import javax.persistence.PrimaryKeyJoinColumn;
9 import javax.persistence.JoinColumn;
10
11 import org.hibernate.AnnotationException;
12 import org.hibernate.util.StringHelper;
13 import org.hibernate.mapping.Column;
14 import org.hibernate.mapping.Join;
15 import org.hibernate.mapping.PersistentClass;
16 import org.hibernate.mapping.SimpleValue;
17 import org.hibernate.mapping.Value;
18
19 /**
20  * Wrap state of an EJB3 @JoinColumn annotation
21  * and build the Hibernate column mapping element
22  *
23  * @author Emmanuel Bernard
24  */

25 public class Ejb3JoinColumn extends Ejb3Column {
26     private String JavaDoc propertyName;
27     /** property name repated to this column */
28     private String JavaDoc referencedColumn;
29     private String JavaDoc mappedBy;
30     private String JavaDoc defaultColumnHeader;
31
32     public void setPropertyName(String JavaDoc propertyName) {
33         this.propertyName = propertyName;
34     }
35
36     public void setReferencedColumn(String JavaDoc referencedColumn) {
37         this.referencedColumn = referencedColumn;
38     }
39
40     public void setDefaultColumnHeader(String JavaDoc defaultColumnHeader) {
41         this.defaultColumnHeader = defaultColumnHeader;
42     }
43
44     public String JavaDoc getPropertyName() {
45         return propertyName;
46     }
47
48     public String JavaDoc getMappedBy() {
49         return mappedBy;
50     }
51
52     public void setMappedBy(String JavaDoc mappedBy) {
53         this.mappedBy = mappedBy;
54     }
55
56     public Ejb3JoinColumn() {
57         setMappedBy(AnnotationBinder.ANNOTATION_STRING_DEFAULT);
58     }
59
60     public Ejb3JoinColumn(String JavaDoc sqlType,
61                           String JavaDoc name,
62                           boolean nullable,
63                           boolean unique,
64                           boolean insertable,
65                           boolean updatable,
66                           String JavaDoc referencedColumn,
67                           String JavaDoc secondaryTable,
68                           Map JavaDoc<String JavaDoc, Join> joins,
69                           PropertyHolder propertyHolder,
70                           String JavaDoc propertyName,
71                           String JavaDoc mappedBy,
72                           boolean isImplicit,
73                           Mappings mappings) {
74         super();
75         setImplicit(isImplicit);
76         setSqlType(sqlType);
77         setColumnName(name);
78         setNullable( nullable );
79         setUnique( unique );
80         setInsertable( insertable );
81         setUpdatable( updatable );
82         setSecondaryTableName( secondaryTable );
83         setPropertyHolder(propertyHolder);
84         setJoins(joins);
85         setMappings(mappings);
86         bind();
87 // super(isImplicit, sqlType,
88
// DEFAULT_COLUMN_LENGTH,
89
// 0,
90
// 0,
91
// name,
92
// nullable,
93
// unique,
94
// insertable,
95
// updatable,
96
// secondaryTable,
97
// joins,
98
// propertyHolder,
99
// mappings);
100
this.referencedColumn = referencedColumn;
101         this.propertyName = propertyName;
102         this.mappedBy = mappedBy;
103     }
104
105     public String JavaDoc getReferencedColumn() {
106         return referencedColumn;
107     }
108
109     public static Ejb3JoinColumn buildJoinColumn(
110             JoinColumn ann,
111             Map JavaDoc<String JavaDoc, Join> joins,
112             PropertyHolder propertyHolder,
113             String JavaDoc propertyName,
114             ExtendedMappings mappings
115             ) {
116         if (ann != null) {
117             Ejb3JoinColumn joinColumn = new Ejb3JoinColumn();
118             joinColumn.setJoinAnnotation(ann, null);
119             joinColumn.setJoins(joins);
120             joinColumn.setPropertyHolder(propertyHolder);
121             joinColumn.setPropertyName(propertyName);
122             joinColumn.setImplicit(false);
123             joinColumn.setMappings(mappings);
124             joinColumn.bind();
125             return joinColumn;
126         }
127         else {
128             Ejb3JoinColumn joinColumn = new Ejb3JoinColumn();
129             joinColumn.setJoins(joins);
130             joinColumn.setPropertyHolder(propertyHolder);
131             joinColumn.setPropertyName(propertyName);
132             joinColumn.setImplicit(true);
133             joinColumn.setMappings(mappings);
134             joinColumn.bind();
135             return joinColumn;
136             //return buildImplicitJoinColumn( AnnotationBinder.ANNOTATION_STRING_DEFAULT, joins, propertyHolder, propertyName, mappings);
137
}
138     }
139
140
141     //FIXME default name still useful in association table
142
public void setJoinAnnotation(JoinColumn annJoin, String JavaDoc defaultName) {
143         if (annJoin == null) {
144             setImplicit(true);
145         }
146         else {
147             setImplicit(false);
148             if ( ! AnnotationBinder.isDefault( annJoin.columnDefinition() ) ) setSqlType( annJoin.columnDefinition() );
149             if ( ! AnnotationBinder.isDefault( annJoin.name() ) ) setColumnName( annJoin.name() );
150             setNullable( annJoin.nullable() );
151             setUnique( annJoin.unique() );
152             setInsertable( annJoin.insertable() );
153             setUpdatable( annJoin.updatable() );
154             setReferencedColumn( annJoin.referencedColumnName() );
155             setSecondaryTableName( annJoin.secondaryTable() );
156         }
157     }
158
159     public static Ejb3JoinColumn buildImplicitJoinColumn(String JavaDoc mappedBy,
160                                                  Map JavaDoc<String JavaDoc, Join> joins,
161                                                  PropertyHolder propertyHolder,
162                                                  String JavaDoc propertyName,
163                                                  ExtendedMappings mappings) {
164         return new Ejb3JoinColumn( (String JavaDoc) null, null,
165                     true, false, true, true, null, (String JavaDoc) null, joins,
166                     propertyHolder, propertyName, mappedBy, true, mappings );
167     }
168
169     /**
170      * Build an Ejb3JoinColumn from a potential annotation and
171      * the identifier it refers to
172      *
173      * @param ann JoinColumn annotation
174      * @param identifier value explaining the identifier
175      * @param propertyHolder
176      * @param mappings mappings
177      *
178      * @return Ejb3JoinColumn
179      */

180     public static Ejb3JoinColumn buildJoinColumn(JoinColumn ann,
181                                                  Value identifier,
182                                                  Map JavaDoc<String JavaDoc, Join> joins,
183                                                  PropertyHolder propertyHolder, ExtendedMappings mappings) {
184             
185         Column col = (Column) identifier.getColumnIterator().next();
186         String JavaDoc defaultName = col.getName();
187         if (ann != null) {
188             String JavaDoc sqlType = ann.columnDefinition().equals("") ? null : ann.columnDefinition();
189             String JavaDoc name = ann.name().equals("") ? defaultName : ann.name();
190             return new Ejb3JoinColumn(sqlType,
191                 name, ann.nullable(), ann.unique(),
192                 ann.insertable(), ann.updatable(),
193                 ann.referencedColumnName(),
194                 ann.secondaryTable(),
195                 joins,
196                     propertyHolder, null, null, false, mappings);
197         }
198         else {
199             return new Ejb3JoinColumn( (String JavaDoc) null, defaultName,
200                     true, false, true, true, null, (String JavaDoc) null,
201                     joins, propertyHolder, null, null, false, mappings
202                 );
203         }
204     }
205
206     public static Ejb3JoinColumn buildJoinColumn(PrimaryKeyJoinColumn ann,
207                                                  Value identifier,
208                                                  Map JavaDoc<String JavaDoc, Join> joins,
209                                                  PropertyHolder propertyHolder, ExtendedMappings mappings) {
210
211         Column col = (Column) identifier.getColumnIterator().next();
212         String JavaDoc defaultName = col.getName();
213         if (ann != null) {
214             String JavaDoc sqlType = ann.columnDefinition().equals("") ? null : ann.columnDefinition();
215             String JavaDoc name = ann.name().equals("") ? defaultName : ann.name();
216             return new Ejb3JoinColumn(sqlType,
217                 name, false, false,
218                 true, true,
219                 ann.referencedColumnName(),
220                 null, joins,
221                 propertyHolder, null, null, false, mappings);
222         }
223         else {
224             return new Ejb3JoinColumn( (String JavaDoc) null, defaultName,
225                     false, false, true, true, null, (String JavaDoc) null,
226                     joins, propertyHolder, null, null, true, mappings
227                 );
228         }
229     }
230     /**
231      * Override persistent class on oneToMany Cases for late settings
232      */

233     public void setPersistentClass(PersistentClass persistentClass) {
234         this.propertyHolder = PropertyHolderBuilder.buildPropertyHolder(persistentClass);
235     }
236
237     public static void checkIfJoinColumn(Object JavaDoc columns, PropertyHolder holder, PropertyInferredData property) {
238         if ( ! ( columns instanceof Ejb3JoinColumn[] ) ) {
239             throw new AnnotationException(
240                 "@Column cannot be used on an association property: "
241                 + holder.getEntityName()
242                 + "."
243                 + property.getPropertyName()
244             );
245         }
246     }
247
248     public void linkValueUsingDefaultColumnNaming(Column column, SimpleValue value) {
249         String JavaDoc columnName;
250         final boolean isAnAssociation = defaultColumnHeader != null || propertyName != null;
251         if (isAnAssociation) {
252             columnName = (defaultColumnHeader == null ? propertyName : defaultColumnHeader) + "_" + column.getName();
253         }
254         else {
255             //is an intra-entity hierarchy table join so copy the name by default
256
columnName = column.getName();
257         }
258         initMappingColumn(
259             columnName,
260             column.getLength(),
261             column.getPrecision(),
262             column.getScale(),
263             getMappingColumn().isNullable(),
264             column.getSqlType(),
265             getMappingColumn().isUnique() );
266         linkWithValue(value);
267     }
268
269     public void linkValueUsingAColumnCopy(Column column, SimpleValue value) {
270         initMappingColumn(
271             column.getName(),
272             column.getLength(),
273             column.getPrecision(),
274             column.getScale(),
275             getMappingColumn().isNullable(),
276             column.getSqlType(),
277             getMappingColumn().isUnique() );
278         linkWithValue(value);
279     }
280
281     //keep it JDK 1.4 compliant
282
//implicit way
283
public static final int NO_REFERENCE = 0;
284     //reference to the pk in an explicit order
285
public static final int PK_REFERENCE = 1;
286     //reference to non pk columns
287
public static final int NON_PK_REFERENCE = 2;
288
289     public static int checkReferencedColumnsType(Ejb3JoinColumn[] columns, PersistentClass referencedEntity) {
290         //convinient container to find whether a column is an id one or not
291
Set JavaDoc<Column> idColumns = new HashSet JavaDoc<Column>();
292         Iterator JavaDoc idColumnsIt = referencedEntity.getIdentifier().getColumnIterator();
293         while ( idColumnsIt.hasNext() ) {
294             idColumns.add( (Column) idColumnsIt.next() );
295         }
296
297         boolean isFkReferencedColumnName = false;
298         boolean noReferencedColumn = true;
299
300         //cheak each referenced column
301
for (Ejb3JoinColumn ejb3Column : columns) {
302             String JavaDoc referencedColumnName = ejb3Column.getReferencedColumn();
303             if (! StringHelper.isEmpty( referencedColumnName ) ) {
304                 noReferencedColumn = false;
305                 Column refCol = new Column(referencedColumnName);
306                 boolean contains = idColumns.contains( refCol );
307                 if ( ! contains ) {
308                     isFkReferencedColumnName = true;
309                 }
310             }
311         }
312         if (isFkReferencedColumnName) {
313             return NON_PK_REFERENCE;
314         }
315         else if (noReferencedColumn) {
316             return NO_REFERENCE;
317         }
318         else {
319             return PK_REFERENCE;
320         }
321     }
322
323 }
Popular Tags