Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 779bf16440e7ee4f49a08ce1414304ff246b4ab5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*******************************************************************************
 * Copyright (c) 2013 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/

package org.eclipse.jpt.jpa.annotate;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jpt.jpa.annotate.mapping.AnnotationAttributeNames;
import org.eclipse.jpt.jpa.annotate.mapping.BasicEntityPropertyElem;
import org.eclipse.jpt.jpa.annotate.mapping.ColumnAttributes;
import org.eclipse.jpt.jpa.annotate.mapping.EntityMappingsDoc;
import org.eclipse.jpt.jpa.annotate.mapping.EntityPropertyElem;
import org.eclipse.jpt.jpa.annotate.mapping.EntityRefPropertyElem;
import org.eclipse.jpt.jpa.annotate.mapping.GeneratedValueAttributes;
import org.eclipse.jpt.jpa.annotate.mapping.IdEntityPropertyElement;
import org.eclipse.jpt.jpa.annotate.mapping.JoinStrategy;
import org.eclipse.jpt.jpa.annotate.mapping.JoinTableAttributes;
import org.eclipse.jpt.jpa.core.MappingKeys;
import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.BasicMapping;
import org.eclipse.jpt.jpa.core.context.Cascade;
import org.eclipse.jpt.jpa.core.context.CollectionMapping;
import org.eclipse.jpt.jpa.core.context.ColumnMapping;
import org.eclipse.jpt.jpa.core.context.Entity;
import org.eclipse.jpt.jpa.core.context.FetchType;
import org.eclipse.jpt.jpa.core.context.GeneratedValue;
import org.eclipse.jpt.jpa.core.context.GenerationType;
import org.eclipse.jpt.jpa.core.context.IdMapping;
import org.eclipse.jpt.jpa.core.context.ManyToManyMapping;
import org.eclipse.jpt.jpa.core.context.ManyToOneMapping;
import org.eclipse.jpt.jpa.core.context.ManyToOneRelationship;
import org.eclipse.jpt.jpa.core.context.OneToManyMapping;
import org.eclipse.jpt.jpa.core.context.OneToOneMapping;
import org.eclipse.jpt.jpa.core.context.OneToOneRelationship;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.RelationshipMapping;
import org.eclipse.jpt.jpa.core.context.SpecifiedColumn;
import org.eclipse.jpt.jpa.core.context.SpecifiedJoinColumn;
import org.eclipse.jpt.jpa.core.context.SpecifiedJoinColumnRelationshipStrategy;
import org.eclipse.jpt.jpa.core.context.SpecifiedJoinTable;
import org.eclipse.jpt.jpa.core.context.SpecifiedJoinTableRelationshipStrategy;
import org.eclipse.jpt.jpa.core.context.SpecifiedMappedByRelationshipStrategy;
import org.eclipse.jpt.jpa.core.context.SpecifiedPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.SpecifiedPrimaryKeyJoinColumn;
import org.eclipse.jpt.jpa.core.context.SpecifiedPrimaryKeyJoinColumnRelationshipStrategy;
import org.eclipse.jpt.jpa.core.jpa2.context.ManyToOneRelationship2_0;
import org.eclipse.jpt.jpa.core.jpa2.context.OneToManyRelationship2_0;
import org.eclipse.jpt.jpa.core.jpa2.context.OneToOneRelationship2_0;
import org.eclipse.jpt.jpa.core.resource.orm.JPA;
import org.eclipse.jpt.jpa.db.Column;
import org.eclipse.jpt.jpa.db.ForeignKey;
import org.eclipse.jpt.jpa.db.Schema;
import org.eclipse.jpt.jpa.db.Table;

public class JavaClassAnnotater 
{
	private PersistentType persistentType;
	private EntityMappingsDoc mappingDoc;
	private DatabaseAnnotationNameBuilder databaseAnnotationNameBuilder = DatabaseAnnotationNameBuilder.Default.INSTANCE;
	private Schema schema;
	private Table table;
	
	public JavaClassAnnotater(PersistentType persistentType, EntityMappingsDoc mappingDoc, Schema schema)
	{
		this.persistentType = persistentType;
		this.mappingDoc = mappingDoc;
		this.schema = schema;
		if (mappingDoc.getTableAttrs().getTableName() != null)
		{
			this.table = this.schema.getTableNamed(mappingDoc.getTableAttrs().getTableName());
		}
	}
	
	public void annotate(IProgressMonitor pm)
	{
		if (this.table != null)
		{
			SubMonitor sm = SubMonitor.convert(pm, mappingDoc.getEntityProperties().length + 1);
			sm.setTaskName(JptJpaAnnotateMessages.ANNOTATE_JAVA_CLASS);
			syncTableAnnotation();
			sm.worked(1);
			for (EntityPropertyElem entityPropertyElem : mappingDoc.getEntityProperties())
			{
				syncPropAnnotation(entityPropertyElem, entityPropertyElem.getPropertyName());
				sm.worked(1);
			}
		}
	}
	
	public DatabaseAnnotationNameBuilder getDatabaseAnnotationNameBuilder() 
	{
		return this.databaseAnnotationNameBuilder;
	}
	
	public void setDatabaseAnnotationNameBuilder(DatabaseAnnotationNameBuilder databaseAnnotationNameBuilder) 
	{
		if (databaseAnnotationNameBuilder == null) 
		{
			throw new NullPointerException("database annotation name builder is required");  //$NON-NLS-1$
		}
		this.databaseAnnotationNameBuilder = databaseAnnotationNameBuilder;
	}
	
	private void syncTableAnnotation() 
	{
		Entity entity = (Entity)this.persistentType.getMapping();
		String tableName = this.databaseAnnotationNameBuilder.buildTableAnnotationName(entity.getName(), table);
		if (tableName != null)
		{
			entity.getTable().setSpecifiedName(tableName);
		}
	}
	
	private void syncPropAnnotation(EntityPropertyElem propElem, String attributeName) 
	{
		SpecifiedPersistentAttribute persistentAttribute = (SpecifiedPersistentAttribute)this.persistentType.getAttributeNamed(attributeName);
		if (propElem == null || propElem.isUnmapped()) 
		{		
			persistentAttribute.setMappingKey(null);
			return;
		}
		String tagName = propElem.getTagName();
		if (tagName.equals(JPA.BASIC)) 
		{
			syncBasicAnnotation(propElem, persistentAttribute);
		} 
//		else if (tagName.equals(JPA.EMBEDDED_TAG)) 
//		{
//			_annotations.deleteClassMemberAnnotations(methName, propName, AnnotationNames.getPropTypeAnnotationNames(AnnotationNames.EMBEDDED_ANNOTATION));
//			_annotations.ensureClassMemberAnnotation(methName, propName, AnnotationNames.EMBEDDED_ANNOTATION);			
//		} 
//		else if (tagName.equals(JPA.EMBEDDED_ID_TAG)) 
//		{
//			_annotations.deleteClassMemberAnnotations(methName, propName, AnnotationNames.getPropTypeAnnotationNames(AnnotationNames.EMBEDDED_ID_ANNOTATION));
//			_annotations.ensureClassMemberAnnotation(methName, propName, AnnotationNames.EMBEDDED_ID_ANNOTATION);			
//		} 
		else if (tagName.equals(JPA.ID)) 
		{
			syncIdAnnotation(propElem, persistentAttribute);
		} 
		else if (tagName.equals(JPA.MANY_TO_MANY)) 
		{
			syncManyToManyAnnotation(propElem, persistentAttribute);
		} 
		else if (tagName.equals(JPA.MANY_TO_ONE)) 
		{
			syncManyToOneAnnotation(propElem, persistentAttribute);
		} 
		else if (tagName.equals(JPA.ONE_TO_MANY))
		{
			syncOneToManyAnnotation(propElem, persistentAttribute);
		} 
		else if (tagName.equals(JPA.ONE_TO_ONE)) 
		{
			syncOneToOneAnnotation(propElem, persistentAttribute);
		} 
//		else if (tagName.equals(JPA.VERSION_TAG)) 
//		{
//			syncVersionAnnotation(propElem, methName, propName);
//		}
	}
	
	private void syncBasicAnnotation(EntityPropertyElem propElem, SpecifiedPersistentAttribute persistentAttribute) 
	{
		/*remove the conflicting annotations*/
		// Since "Basic" is the default mapping, we can just clear the mapping annotation
		AttributeMapping mapping = persistentAttribute.setMappingKey(null);
		BasicMapping basicMapping = (BasicMapping)mapping;		
		assert propElem instanceof BasicEntityPropertyElem;
		BasicEntityPropertyElem basicElem = (BasicEntityPropertyElem)propElem;
		if (basicElem.getTemporalType() != null)
		{
			// TODO Set Temporal annotation
		}
				
		syncColumnAnnotation(propElem, basicMapping);
	}
		
	private void syncIdAnnotation(EntityPropertyElem propElem, SpecifiedPersistentAttribute persistentAttribute) 
	{
		AttributeMapping mapping = persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY);
		IdMapping idMapping = (IdMapping) mapping;
		
		assert propElem instanceof IdEntityPropertyElement;
		IdEntityPropertyElement idElem = (IdEntityPropertyElement)propElem;
		GeneratedValueAttributes genAttrs = idElem.getGeneratedValueAttrs();
		if (genAttrs != null)
		{
			GeneratedValue gv = idMapping.addGeneratedValue();
			if (genAttrs.getStrategy() != null)
			{
				GenerationType type = getGenerationType(genAttrs.getStrategy());
				if (type != null && type != gv.getDefaultStrategy())
				{
					gv.setSpecifiedStrategy(type);
				}				
			}
			if (genAttrs.getGenerator() != null && genAttrs.getGenerator().equals(gv.getDefaultGenerator()))
			{
				gv.setSpecifiedGenerator(genAttrs.getGenerator());
			}
		}
		
		syncColumnAnnotation(propElem, idMapping);
	}
	
	private void syncColumnAnnotation(EntityPropertyElem propElem, ColumnMapping columnMapping) 
	{
		ColumnAttributes colAttrs = propElem.getColumnAnnotationAttrs();
		SpecifiedColumn column = columnMapping.getColumn();
		if (propElem.getDBColumn() != null)
		{
			Column dbColumn = propElem.getDBColumn();
			String colName = this.databaseAnnotationNameBuilder.buildColumnAnnotationName(propElem.getPropertyName(), dbColumn);
			// Setting col name, unique, nullable, length, precision, scale, insertable, updatable
			// only when it's not the default mapping.
			if (colName != null)
			{
				// Column Name
				column.setSpecifiedName(colName);
				// Unique
				if (colAttrs.isSetUnique() && colAttrs.isUnique())
				{
					column.setSpecifiedUnique(colAttrs.isUnique());
				}
				// Nullable
				if (colAttrs.isSetNullable() && !colAttrs.isNullable())
				{
					column.setSpecifiedNullable(colAttrs.isNullable());
				}
				//Length
				if (colAttrs.isSetLength() && colAttrs.getLength() != column.getDefaultLength())
				{
					column.setSpecifiedLength(colAttrs.getLength());
				}
				// Precision
				if (colAttrs.isSetPrecision() && colAttrs.getPrecision() != column.getDefaultPrecision())
				{
					column.setSpecifiedPrecision(colAttrs.getPrecision());
				}
				// Scale
				if (colAttrs.isSetScale() && colAttrs.getScale() != column.getDefaultScale())
				{
					column.setSpecifiedScale(colAttrs.getScale());
				}
				// Insertable
				if (colAttrs.isSetInsertable() && !colAttrs.isInsertable())
				{
					column.setSpecifiedInsertable(colAttrs.isInsertable());
				}
				// Updatable
				if (colAttrs.isSetUpdatable() && !colAttrs.isUpdatable())
				{
					column.setSpecifiedUpdatable(colAttrs.isUpdatable());
				}
			}
		}
		// ColumnDefinition
		if (colAttrs != null && colAttrs.getColumnDefinition() != null)
		{
			column.setColumnDefinition(colAttrs.getColumnDefinition());
		}
	}
	
	private void syncManyToOneAnnotation(EntityPropertyElem propElem, SpecifiedPersistentAttribute persistentAttribute) 
	{
		assert propElem instanceof EntityRefPropertyElem;
		EntityRefPropertyElem refPropElem = (EntityRefPropertyElem)propElem;

		AttributeMapping mapping = persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY);
		ManyToOneMapping mtoMapping = (ManyToOneMapping)mapping;
		
		// Target Entity
		if (refPropElem.getRefEntityClassName() != null && 
				!refPropElem.getRefEntityClassName().equals(mtoMapping.getDefaultTargetEntity()))
		{
			mtoMapping.setSpecifiedTargetEntity(refPropElem.getRefEntityClassName());
		}
		
		// Fetch
		if (propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH) != null)
		{
			FetchType fetchType = getFetchType(propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH).attrValue);
			if (fetchType != null && fetchType != mtoMapping.getDefaultFetch())
			{
				mtoMapping.setSpecifiedFetch(fetchType);
			}
		}
		
		// Cascade
		syncCascadeMember(mtoMapping, refPropElem);		
				        
		if (!isJpa1_0Project() && refPropElem.getJoinTable() != null)
		{
			ManyToOneRelationship relation = mtoMapping.getRelationship();
			ManyToOneRelationship2_0 relation2 = (ManyToOneRelationship2_0)relation;
			relation2.setStrategyToJoinTable();
			SpecifiedJoinTableRelationshipStrategy joinTableStrategy = relation2.getJoinTableStrategy();
			syncJoinTableAnnotation(refPropElem, joinTableStrategy);
		}
		else if (!refPropElem.getJoinColumns().isEmpty())
		{
			mtoMapping.getRelationship().setStrategyToJoinColumn();
			SpecifiedJoinColumnRelationshipStrategy joinColumnStrategy = mtoMapping.getRelationship().getJoinColumnStrategy();
			syncJoinColumnsAnnotations(refPropElem.getDBTable(), refPropElem.getReferencedTable(),
					refPropElem.getPropertyName(), refPropElem.getJoinColumns(), joinColumnStrategy);
		}		
	}
	
	private void syncOneToManyAnnotation(EntityPropertyElem propElem, SpecifiedPersistentAttribute persistentAttribute) 
	{
		assert propElem instanceof EntityRefPropertyElem;
		EntityRefPropertyElem refPropElem = (EntityRefPropertyElem)propElem;
		
		AttributeMapping mapping = persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY);
		OneToManyMapping otmMapping = (OneToManyMapping)mapping;
		
		// Target Entity
		if (refPropElem.getRefEntityClassName() != null && 
				!refPropElem.getRefEntityClassName().equals(otmMapping.getDefaultTargetEntity()))
		{
			otmMapping.setSpecifiedTargetEntity(refPropElem.getRefEntityClassName());
		}
		
		// Fetch
		if (propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH) != null)
		{
			FetchType fetchType = getFetchType(propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH).attrValue);
			if (fetchType != null && fetchType != otmMapping.getDefaultFetch())
			{
				otmMapping.setSpecifiedFetch(fetchType);
			}
		}
		
		// Cascade
		syncCascadeMember(otmMapping, refPropElem);	
		
		syncToManyAnnotations(propElem, otmMapping);
				
		if (propElem.getAnnotationAttribute(AnnotationAttributeNames.MAPPEDBY) != null)
		{
			// Mapped by
			otmMapping.getRelationship().setStrategyToMappedBy();
			SpecifiedMappedByRelationshipStrategy mappedByStrategy = otmMapping.getRelationship().getMappedByStrategy();
			mappedByStrategy.setMappedByAttribute(propElem.getAnnotationAttribute(AnnotationAttributeNames.MAPPEDBY).attrValue);
		}
		else if (refPropElem.getJoinTable() != null)
		{
			// Join Table
			otmMapping.getRelationship().setStrategyToJoinTable();
			SpecifiedJoinTableRelationshipStrategy joinTableStrategy = otmMapping.getRelationship().getJoinTableStrategy();
			syncJoinTableAnnotation(refPropElem, joinTableStrategy);
		}
		else if (!isJpa1_0Project() && !refPropElem.getJoinColumns().isEmpty())
		{
			// Join columns
			SpecifiedJoinColumnRelationshipStrategy joinColumnStrategy = null;
			if (otmMapping.getRelationship() instanceof OneToManyRelationship2_0)
			{
				OneToManyRelationship2_0 relation2_0 = (OneToManyRelationship2_0)otmMapping.getRelationship();
				relation2_0.setStrategyToJoinColumn();
				joinColumnStrategy = relation2_0.getJoinColumnStrategy();
			}
			if (joinColumnStrategy != null)
			{
				syncJoinColumnsAnnotations(refPropElem.getDBTable(), refPropElem.getReferencedTable(),
						refPropElem.getPropertyName(), refPropElem.getJoinColumns(), joinColumnStrategy);
			}
		}
	}
	
	private void syncOneToOneAnnotation(EntityPropertyElem propElem, SpecifiedPersistentAttribute persistentAttribute) 
	{
		assert propElem instanceof EntityRefPropertyElem;
		EntityRefPropertyElem refPropElem = (EntityRefPropertyElem)propElem;
		AttributeMapping mapping = persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY);
		OneToOneMapping otoMapping = (OneToOneMapping)mapping;
		
		// Target Entity
		if (refPropElem.getRefEntityClassName() != null && 
				!refPropElem.getRefEntityClassName().equals(otoMapping.getDefaultTargetEntity()))
		{
			otoMapping.setSpecifiedTargetEntity(refPropElem.getRefEntityClassName());
		}
		
		// Fetch
		if (propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH) != null)
		{
			FetchType fetchType = getFetchType(propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH).attrValue);
			if (fetchType != null && fetchType != otoMapping.getDefaultFetch())
			{
				otoMapping.setSpecifiedFetch(fetchType);
			}
		}
		
		// Cascade
		syncCascadeMember(otoMapping, refPropElem);		

		if (propElem.getAnnotationAttribute(AnnotationAttributeNames.MAPPEDBY) != null)
		{
			// Mapped by
			otoMapping.getRelationship().setStrategyToMappedBy();
			SpecifiedMappedByRelationshipStrategy mappedByStrategy = otoMapping.getRelationship().getMappedByStrategy();
			mappedByStrategy.setMappedByAttribute(propElem.getAnnotationAttribute(AnnotationAttributeNames.MAPPEDBY).attrValue);
		}		
		else if (!refPropElem.getJoinColumns().isEmpty())
		{
			// Join columns			
			otoMapping.getRelationship().setStrategyToJoinColumn();
			SpecifiedJoinColumnRelationshipStrategy joinColumnStrategy = otoMapping.getRelationship().getJoinColumnStrategy();
			syncJoinColumnsAnnotations(refPropElem.getDBTable(), refPropElem.getReferencedTable(),
					refPropElem.getPropertyName(), refPropElem.getJoinColumns(), joinColumnStrategy);
		}
		else if (refPropElem.getJoinStrategy() == JoinStrategy.PRIMARY_KEY_JOIN_COLUMNS)
		{
			// Primary Key Join Columns
			otoMapping.getRelationship().setStrategyToPrimaryKeyJoinColumn();
			SpecifiedPrimaryKeyJoinColumnRelationshipStrategy pkJoinStrategy = otoMapping.getRelationship().getPrimaryKeyJoinColumnStrategy();
			syncPkJoinColumnAnnotations(refPropElem.getPkJoinColumns(), pkJoinStrategy);
		}
		else if (!isJpa1_0Project() && refPropElem.getJoinTable() != null)
		{
			// Join table
			OneToOneRelationship relation = otoMapping.getRelationship();
			OneToOneRelationship2_0 relation2 = (OneToOneRelationship2_0)relation;
			relation2.setStrategyToJoinTable();
			SpecifiedJoinTableRelationshipStrategy joinTableStrategy = relation2.getJoinTableStrategy();
			syncJoinTableAnnotation(refPropElem, joinTableStrategy);
		}
	}
	
	private void syncManyToManyAnnotation(EntityPropertyElem propElem, SpecifiedPersistentAttribute persistentAttribute) 
	{
		assert propElem instanceof EntityRefPropertyElem;
		EntityRefPropertyElem refPropElem = (EntityRefPropertyElem)propElem;		
		AttributeMapping mapping = persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY);
		ManyToManyMapping mtmMapping = (ManyToManyMapping)mapping;
		
		// Target Entity
		if (refPropElem.getRefEntityClassName() != null && 
				!refPropElem.getRefEntityClassName().equals(mtmMapping.getDefaultTargetEntity()))
		{
			mtmMapping.setSpecifiedTargetEntity(refPropElem.getRefEntityClassName());
		}
		
		// Fetch
		if (propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH) != null)
		{
			FetchType fetchType = getFetchType(propElem.getAnnotationAttribute(AnnotationAttributeNames.FETCH).attrValue);
			if (fetchType != null && fetchType != mtmMapping.getDefaultFetch())
			{
				mtmMapping.setSpecifiedFetch(fetchType);
			}
		}
		
		// Cascade
		syncCascadeMember(mtmMapping, refPropElem);		
		
		syncToManyAnnotations(propElem, mtmMapping);
		
		// Join Strategy
		if (refPropElem.getAnnotationAttribute(AnnotationAttributeNames.MAPPEDBY) != null)
		{
			// Mapped By
			mtmMapping.getRelationship().setStrategyToMappedBy();
			SpecifiedMappedByRelationshipStrategy mappedByStrategy = mtmMapping.getRelationship().getMappedByStrategy();
			mappedByStrategy.setMappedByAttribute(propElem.getAnnotationAttribute(AnnotationAttributeNames.MAPPEDBY).attrValue);
		}
		else if (refPropElem.getJoinTable() != null)
		{
			// Join table
			mtmMapping.getRelationship().setStrategyToJoinTable();
			SpecifiedJoinTableRelationshipStrategy joinTableStrategy = mtmMapping.getRelationship().getJoinTableStrategy();
			syncJoinTableAnnotation(refPropElem, joinTableStrategy);
		}		
		
	}
	
	private void syncCascadeMember(RelationshipMapping relationMapping, EntityRefPropertyElem refPropElem) 
	{
		Cascade cascade = relationMapping.getCascade();
		// Clear the exising cascade
		cascade.setAll(false);
		cascade.setMerge(false);
		cascade.setPersist(false);
		cascade.setRefresh(false);
		cascade.setRemove(false);

		List<String> cascadeStrs = refPropElem.getAllCascades();
		for (String cascadeStr : cascadeStrs)
		{
			if (cascadeStr.equals(JPA.CASCADE_ALL)) 
			{
				cascade.setAll(true);
			}
			if (cascadeStr.equals(JPA.CASCADE_PERSIST)) 
			{
				cascade.setPersist(true);
			}
			if (cascadeStr.equals(JPA.CASCADE_MERGE)) 
			{
				cascade.setMerge(true);
			}
			if (cascadeStr.equals(JPA.CASCADE_REMOVE)) 
			{
				cascade.setRemove(true);
			}
			if (cascadeStr.equals(JPA.CASCADE_REFRESH)) 
			{
				cascade.setRefresh(true);
			}
		}
	}
	
	private void syncJoinTableAnnotation(EntityRefPropertyElem refPropEmElem, SpecifiedJoinTableRelationshipStrategy jtRelation) 
	{
		JoinTableAttributes joinTableAttrs = refPropEmElem.getJoinTable();
		assert joinTableAttrs != null;
		SpecifiedJoinTable joinTable = jtRelation.getJoinTable();
		Table dbJoinTable = null;
		// Name
		if (joinTableAttrs.getTableName() != null)
		{
			dbJoinTable = this.schema.getTableNamed(joinTableAttrs.getTableName());
			if (dbJoinTable != null)
			{
				String joinTableName = this.databaseAnnotationNameBuilder.buildJoinTableAnnotationName(dbJoinTable);
				joinTable.setSpecifiedName(joinTableName != null ? joinTableName : dbJoinTable.getName());
			}
		}
		
		List<ColumnAttributes> cols = joinTableAttrs.getJoinColumns();
		for (ColumnAttributes col : cols)
		{
			SpecifiedJoinColumn joinColumn = joinTable.addSpecifiedJoinColumn();
			
			syncJoinColumnAnnotations(true, dbJoinTable, refPropEmElem.getDBTable(), 
					refPropEmElem.getPropertyName(), col, joinColumn);
		}
		List<ColumnAttributes> inverseCols = joinTableAttrs.getInverseJoinColumns();
		for (ColumnAttributes col : inverseCols)
		{
			SpecifiedJoinColumn joinColumn = joinTable.addSpecifiedInverseJoinColumn();
			syncJoinColumnAnnotations(true, dbJoinTable, refPropEmElem.getReferencedTable(), refPropEmElem.getPropertyName(),
					col, joinColumn);
		}
	}
	
	private void syncJoinColumnAnnotations(boolean isJoinTable, Table baseTable, Table refTable, String propName, 
			ColumnAttributes col, SpecifiedJoinColumn joinColumn)
	{
		if (baseTable == null || refTable == null)
		{
			return;
		}
		boolean printoutNameOrRefName = false;
		if (col.getName() != null)
		{
			Column dbColumn = baseTable.getColumnNamed(col.getName());
			ForeignKey fk = getForeignKey(dbColumn);
			Column refDbCol = col.getReferencedColumnName() != null ? 
					refTable.getColumnNamed(col.getReferencedColumnName()) : null;

			if ((fk != null && !fk.referencesSingleColumnPrimaryKey()) || isJoinTable)
			{
				if (refDbCol != null)
				{
					// need to print out both columnName and referenceColumnName
					String colName = this.databaseAnnotationNameBuilder.buildJoinColumnAnnotationName(dbColumn);
					String refColName = this.databaseAnnotationNameBuilder.buildJoinColumnAnnotationName(refDbCol);
					joinColumn.setSpecifiedName(colName);
					joinColumn.setSpecifiedReferencedColumnName(refColName);
					printoutNameOrRefName = true;
				}				
			}
			else
			{
				// No need to print out referenceColumnName. Print out columnName if 
				// the name cannot be defaulted
				String colName = this.databaseAnnotationNameBuilder.buildColumnAnnotationName(propName, dbColumn);
				if (colName != null)
				{
					joinColumn.setSpecifiedName(colName);
					printoutNameOrRefName = true;
				}
			}				
		}
		if (printoutNameOrRefName)
		{
			// Unique
			if (col.isSetUnique() && col.isUnique())
			{
				joinColumn.setSpecifiedUnique(col.isUnique());
			}
			// Nullable
			if (col.isSetNullable() && !col.isNullable())
			{
				joinColumn.setSpecifiedNullable(col.isNullable());
			}
			// Insertable
			if (col.isSetInsertable() && !col.isInsertable())
			{
				joinColumn.setSpecifiedInsertable(col.isInsertable());
			}
			// Updatable
			if (col.isSetUpdatable() && !col.isUpdatable())
			{
				joinColumn.setSpecifiedUpdatable(col.isUpdatable());
			}
			// Column Definition
			if (col.getColumnDefinition() != null)
			{
				joinColumn.setColumnDefinition(col.getColumnDefinition());
			}
		}
	}
	
	private void syncJoinColumnsAnnotations(Table baseTable, Table refTable, String propertyName, List<ColumnAttributes> joinColumns, SpecifiedJoinColumnRelationshipStrategy joinColumnStrategy) 
	{		
		if (joinColumns == null || joinColumns.size() == 0) 
		{
			return;
		}
		int count = joinColumns.size();
		/*remove empty join column tags so that no annotation is generated for them*/
		for (int i = count-1; i >= 0; --i) 
		{
			if (joinColumns.get(i).isEmpty()) 
			{
				joinColumns.remove(i);
			}
		}
		count = joinColumns.size(); //recompute the size just in case any tag has been removed from the list above
		
		for (int i = 0; i < count; ++i)
		{
			ColumnAttributes col = joinColumns.get(i);
			SpecifiedJoinColumn joinColumn = joinColumnStrategy.addSpecifiedJoinColumn();
			syncJoinColumnAnnotations(false, baseTable, refTable, propertyName, col, joinColumn);
		}
	}
	
	/**
	 * Sync the MapBy and OrderBy annotations.
	 */
	private void syncToManyAnnotations(EntityPropertyElem propElem,	CollectionMapping collectionMapping) 
	{
		assert propElem instanceof EntityPropertyElem;
		EntityRefPropertyElem refPropElem = (EntityRefPropertyElem)propElem;
		// TODO @MapKey
		
		// Order by
		if (refPropElem.getOrderBy() != null)
		{
			collectionMapping.getOrderable().setSpecifiedOrderBy(refPropElem.getOrderBy());
		}
	}
	
	private void syncPkJoinColumnAnnotations(List<ColumnAttributes> pkJoinCols, SpecifiedPrimaryKeyJoinColumnRelationshipStrategy pkJoinStrategy) 
	{
		for (ColumnAttributes pkJoinCol : pkJoinCols) 
		{
			SpecifiedPrimaryKeyJoinColumn pkJoinColumn = pkJoinStrategy.addPrimaryKeyJoinColumn();
			// Name
			if (pkJoinCol.getName() != null && !pkJoinCol.getName().equals(pkJoinColumn.getDefaultName()))
			{
				pkJoinColumn.setSpecifiedName(pkJoinCol.getName());
			}
			// Referenced Column Name
			if (pkJoinCol.getReferencedColumnName() != null && 
					!pkJoinCol.getReferencedColumnName().equals(pkJoinColumn.getDefaultReferencedColumnName()))
			{
				pkJoinColumn.setSpecifiedReferencedColumnName(pkJoinCol.getReferencedColumnName());
			}
			// Column Definition
			if (pkJoinCol.getColumnDefinition() != null)
			{
				pkJoinColumn.setColumnDefinition(pkJoinCol.getColumnDefinition());
			}			
		}
	}
	
	private GenerationType getGenerationType(String strategy)
	{
		if (strategy.equalsIgnoreCase("auto"))
		{
			return GenerationType.AUTO;
		}
		else if (strategy.equalsIgnoreCase("identity"))
		{
			return GenerationType.IDENTITY;
		}
		else if (strategy.equalsIgnoreCase("sequence"))
		{
			return GenerationType.SEQUENCE;
		}
		else if (strategy.equalsIgnoreCase("table"))
		{
			return GenerationType.TABLE;
		}
		return null;
	}
	
	private FetchType getFetchType(String fetch)
	{
		if (fetch == null)
		{
			return null;
		}
		else if (fetch.equalsIgnoreCase("eager"))
		{
			return FetchType.EAGER;
		}
		else if (fetch.equalsIgnoreCase("lazy"))
		{
			return FetchType.LAZY;
		}
		else 
		{
			return null;
		}
	}
	
	private boolean isJpa1_0Project()
	{
        return this.persistentType.getPersistenceUnit().getJpaPlatform().getJpaVersion().getVersion().equals("1.0");
	}
	
	private ForeignKey getForeignKey(Column dbColumn)
	{
		if( dbColumn== null || !dbColumn.isPartOfForeignKey())
		{
			return null;
		}
		ForeignKey foreignKey = null;
		Iterable<ForeignKey> it = dbColumn.getTable().getForeignKeys();
		Iterator<ForeignKey> i = it.iterator();
		while( i.hasNext() )
		{
			ForeignKey fk = i.next();
			Column c = fk.getBaseColumns().iterator().next();
			if( c.equals( dbColumn ) )
			{
				foreignKey = fk;
				break;
			}
		}
		return foreignKey;
	}
			
}

Back to the top