Skip to main content
summaryrefslogtreecommitdiffstats
blob: 99ed3e84f46c4261f8b581c8804131a0393fa365 (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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
/*******************************************************************************
 * Copyright (c) 2006, 2009 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.gen.internal.old;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.lang.reflect.Modifier;
import com.ibm.icu.text.Collator;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.db.Column;
import org.eclipse.jpt.db.ForeignKey;
import org.eclipse.jpt.db.Table;
import org.eclipse.jpt.utility.JavaType;
import org.eclipse.jpt.utility.internal.BooleanReference;
import org.eclipse.jpt.utility.internal.IndentingPrintWriter;
import org.eclipse.jpt.utility.internal.NameTools;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterators.FilteringIterator;
import org.eclipse.osgi.util.NLS;

// TODO format generated code per preferences
// TODO organize generated imports per preferences
/**
 * This generator will generate an entity for a table.
 */
public class EntityGenerator {
	final Config config;
	private final IPackageFragment packageFragment;
	private final GenTable genTable;
	private final String entityClassName;
	private final String pkClassName;


	// ********** public API **********

	static void generateEntity(
			Config config,
			IPackageFragment packageFragment,
			GenTable genTable,
			IProgressMonitor progressMonitor
	) {
		if ((config == null) || (packageFragment == null) || (genTable == null)) {
			throw new NullPointerException();
		}
		new EntityGenerator(config, packageFragment, genTable).generateEntity(progressMonitor);
	}


	// ********** constructor/initialization **********

	private EntityGenerator(Config config, IPackageFragment packageFragment, GenTable genTable) {
		super();
		this.config = config;
		this.packageFragment = packageFragment;
		this.genTable = genTable;
		this.entityClassName = this.fullyQualify(this.getEntityName());
		this.pkClassName = this.entityClassName + '.' + config.getPrimaryKeyMemberClassName();
	}


	// ********** code gen **********

	private void generateEntity(IProgressMonitor progressMonitor) {
		try {
			this.generateEntity_(progressMonitor);
		} catch (JavaModelException ex) {
			throw new RuntimeException(ex);
		}
	}

	private void generateEntity_(IProgressMonitor progressMonitor) throws JavaModelException {
		SubMonitor sm = SubMonitor.convert(progressMonitor, this.buildTaskName(), 100);
		String fileName = this.getEntityName() + ".java";  //$NON-NLS-1$
		String source = this.buildSource();
		sm.worked(20);
		try {
			this.packageFragment.createCompilationUnit(fileName, source, false, sm.newChild(40));
		} catch (JavaModelException ex) {
			if (ex.getJavaModelStatus().getCode() == IJavaModelStatusConstants.NAME_COLLISION) {
				if (this.config.getOverwriteConfirmer().overwrite(this.entityClassName)) {
					this.packageFragment.createCompilationUnit(fileName, source, true, sm.newChild(40));
				}
			} else {
				throw ex;
			}
		}
		sm.setWorkRemaining(0);
	}

	private String buildTaskName() {
		return NLS.bind(JptGenMessages.EntityGenerator_taskName, this.getEntityName());
	}

	/**
	 * build the "body" source first; then build the "package" and "imports" source
	 * and concatenate the "body" source to it
	 */
	private String buildSource() {
		// build the body source first so we can gather up the import statements
		BodySource bodySource = this.buildBodySource();

		StringWriter sw = new StringWriter(bodySource.length() + 2000);
		PrintWriter pw = new PrintWriter(sw);
		this.printPackageAndImportsOn(pw, bodySource);
		pw.print(bodySource.getSource());
		return sw.toString();
	}

	private BodySource buildBodySource() {
		EntitySourceWriter pw = new EntitySourceWriter(this.getPackageName(), this.entityClassName);
		this.printBodySourceOn(pw);
		return pw;
	}

	private void printBodySourceOn(EntitySourceWriter pw) {
		this.printClassDeclarationOn(pw);

		pw.indent();
			this.printEntityPrimaryKeyFieldsOn(pw);
			this.printEntityNonPrimaryKeyBasicFieldsOn(pw);
			this.printEntityManyToOneFieldsOn(pw);
			this.printEntityOneToManyFieldsOn(pw);
			this.printEntityOwnedManyToManyFieldsOn(pw);
			this.printEntityNonOwnedManyToManyFieldsOn(pw);
			this.printSerialVersionUIDFieldOn(pw);
			pw.println();

			this.printZeroArgumentConstructorOn(this.getEntityName(), this.config.getMethodVisibilityClause(), pw);
			if (this.config.propertyAccessType() || this.config.generateGettersAndSetters()) {
				this.printEntityPrimaryKeyPropertiesOn(pw);
				this.printEntityNonPrimaryKeyBasicPropertiesOn(pw);
				this.printEntityManyToOnePropertiesOn(pw);
				this.printEntityOneToManyPropertiesOn(pw);
				this.printEntityOwnedManyToManyPropertiesOn(pw);
				this.printEntityNonOwnedManyToManyPropertiesOn(pw);
			}

			if (this.primaryKeyClassIsRequired()) {
				this.printPrimaryKeyClassOn(pw);
			}
		pw.undent();

		pw.print('}');
		pw.println();  // EOF
	}


	// ********** class declaration **********

	private void printClassDeclarationOn(EntitySourceWriter pw) {
		this.printEntityAnnotationOn(pw);
		this.printTableAnnotationOn(pw);
		this.printIdClassAnnotationOn(pw);

		pw.print("public class ");  //$NON-NLS-1$
		pw.printTypeDeclaration(this.entityClassName);
		if (config.serializable()) {
			pw.print(" implements ");  //$NON-NLS-1$
			pw.printTypeDeclaration(Serializable.class.getName());
		}
		pw.print(" {");  //$NON-NLS-1$
		pw.println();
	}

	private void printEntityAnnotationOn(EntitySourceWriter pw) {
		pw.printAnnotation(JPA.ENTITY);
		pw.println();
	}

	private void printTableAnnotationOn(EntitySourceWriter pw) {
		String tableName = this.config.getDatabaseAnnotationNameBuilder().buildTableAnnotationName(this.getEntityName(), this.getTable());
		if (tableName == null) {
			return;  // the default table name is OK
		}
		pw.printAnnotation(JPA.TABLE);
		pw.print("(name=");  //$NON-NLS-1$
		pw.printStringLiteral(tableName);
		pw.print(')');
		pw.println();
	}

	private void printIdClassAnnotationOn(EntitySourceWriter pw) {
		if (this.primaryKeyClassIsRequired() && this.config.generateIdClassForCompoundPK()) {
			pw.printAnnotation(JPA.ID_CLASS);
			pw.print('(');
			pw.printTypeDeclaration(this.pkClassName);
			pw.print(".class)");  //$NON-NLS-1$
			pw.println();
		}
	}


	// ********** primary key fields **********

	private void printEntityPrimaryKeyFieldsOn(EntitySourceWriter pw) {
		if (this.primaryKeyClassIsRequired() && this.config.generateEmbeddedIdForCompoundPK()) {
			this.printEntityEmbeddedIdPrimaryKeyFieldOn(pw);
		} else {
			this.printEntityReadOnlyPrimaryKeyFieldsOn(pw);
			this.printEntityWritablePrimaryKeyFieldsOn(pw);
		}
	}

	private void printEntityEmbeddedIdPrimaryKeyFieldOn(EntitySourceWriter pw) {
		if (this.config.fieldAccessType()) {
			pw.printAnnotation(JPA.EMBEDDED_ID);
			pw.println();
		}
		this.printFieldOn(this.genTable.getAttributeNameForEmbeddedId(), this.pkClassName, pw);
	}

	private void printEntityReadOnlyPrimaryKeyFieldsOn(EntitySourceWriter pw) {
		this.printPrimaryKeyFieldsOn(pw, true, true);  // true=read-only; true=print ID annotation on fields
	}

	private void printEntityWritablePrimaryKeyFieldsOn(EntitySourceWriter pw) {
		this.printPrimaryKeyFieldsOn(pw, false, true);  // false=writable; true=print ID annotation on fields
	}

	private void printPrimaryKeyFieldsOn(EntitySourceWriter pw, boolean readOnly, boolean printIdAnnotation) {
		for (Iterator<Column> stream = this.primaryKeyColumns(readOnly); stream.hasNext(); ) {
			this.printPrimaryKeyFieldOn(stream.next(), pw, readOnly, printIdAnnotation);
		}
	}

	private Iterator<Column> primaryKeyColumns(boolean readOnly) {
		return readOnly ? this.genTable.readOnlyPrimaryKeyColumns() : this.genTable.writablePrimaryKeyColumns();
	}

	// TODO if the field's type is java.util/sql.Date, it needs @Temporal(DATE)
	// TODO if the primary key is auto-generated, the field must be an integral type
	private void printPrimaryKeyFieldOn(Column column, EntitySourceWriter pw, boolean readOnly, boolean printIdAnnotation) {
		String fieldName = this.genTable.getAttributeNameFor(column);
		if (this.config.fieldAccessType()) {
			if (printIdAnnotation) {
				pw.printAnnotation(JPA.ID);
				pw.println();
			}
			String columnName = this.config.getDatabaseAnnotationNameBuilder().buildColumnAnnotationName(fieldName, column);
			if (readOnly) {
				this.printReadOnlyColumnAnnotationOn(columnName, pw);
			} else {
				this.printColumnAnnotationOn(columnName, pw);
			}
		}
		this.printFieldOn(fieldName, column.getPrimaryKeyJavaTypeDeclaration(), pw);
	}

	private void printReadOnlyColumnAnnotationOn(String columnName, EntitySourceWriter pw) {
		pw.printAnnotation(JPA.COLUMN);
		pw.print('(');
		if (columnName != null) {
			pw.print("name=");  //$NON-NLS-1$
			pw.printStringLiteral(columnName);
			pw.print(", ");  //$NON-NLS-1$
		}
		pw.print("insertable=false, updatable=false)");  //$NON-NLS-1$
		pw.println();
	}


	// ********** basic fields **********

	private void printEntityNonPrimaryKeyBasicFieldsOn(EntitySourceWriter pw) {
		for (Iterator<Column> stream = this.genTable.nonPrimaryKeyBasicColumns(); stream.hasNext(); ) {
			this.printEntityNonPrimaryKeyBasicFieldOn(stream.next(), pw);
		}
	}

	private void printEntityNonPrimaryKeyBasicFieldOn(Column column, EntitySourceWriter pw) {
		String fieldName = this.genTable.getAttributeNameFor(column);
		if (this.config.fieldAccessType()) {
			String columnName = this.config.getDatabaseAnnotationNameBuilder().buildColumnAnnotationName(fieldName, column);
			this.printColumnAnnotationOn(columnName, pw);
		}
		if (column.isLOB()) {
			pw.printAnnotation(JPA.LOB);
			pw.println();
		}
		this.printFieldOn(fieldName, column.getJavaTypeDeclaration(), pw);
	}

	private void printColumnAnnotationOn(String columnName, EntitySourceWriter pw) {
		if (columnName != null) {  // the column name is null if the default is OK
			pw.printAnnotation(JPA.COLUMN);
			pw.print("(name=");  //$NON-NLS-1$
			pw.printStringLiteral(columnName);
			pw.print(')');
			pw.println();
		}
	}


	// ********** many-to-one fields **********

	private void printEntityManyToOneFieldsOn(EntitySourceWriter pw) {
		for (Iterator<ManyToOneRelation> stream = this.genTable.manyToOneRelations(); stream.hasNext(); ) {
			this.printEntityManyToOneFieldOn(stream.next(), pw);
		}
	}

	private void printEntityManyToOneFieldOn(ManyToOneRelation relation, EntitySourceWriter pw) {
		String fieldName = this.genTable.getAttributeNameFor(relation);
		if (this.config.fieldAccessType()) {
			this.printManyToOneAnnotationOn(fieldName, relation, pw);
		}
		this.printFieldOn(fieldName, this.fullyQualify(relation.getReferencedEntityName()), pw);
	}

	private void printManyToOneAnnotationOn(String attributeName, ManyToOneRelation relation, EntitySourceWriter pw) {
		pw.printAnnotation(JPA.MANY_TO_ONE);
		pw.println();
		ForeignKey foreignKey = relation.getForeignKey();
		if (foreignKey.referencesSingleColumnPrimaryKey()) {
			// if the FK references a single-column PK, 'referencedColumnName' is not required
			String joinColumnName = this.config.getDatabaseAnnotationNameBuilder().buildJoinColumnAnnotationName(attributeName, foreignKey);
			if (joinColumnName == null) {
				// no JoinColumn annotation needed: the default 'name' and 'referencedColumnName' work
			} else {
				// there is only a single join column here (just not the default name)
				this.printJoinColumnAnnotationOn(joinColumnName, null, pw);
				pw.println();
			}
		} else {
			this.printManyToOneJoinColumnsAnnotationOn(foreignKey, pw);
		}
	}

	private void printManyToOneJoinColumnsAnnotationOn(ForeignKey foreignKey, EntitySourceWriter pw) {
		if (foreignKey.columnPairsSize() > 1) {
			pw.printAnnotation(JPA.JOIN_COLUMNS);
			pw.print("({");  //$NON-NLS-1$
			pw.println();
			pw.indent();
		}
		this.printJoinColumnAnnotationsOn(foreignKey, pw);
		if (foreignKey.columnPairsSize() > 1) {
			pw.undent();
			pw.println();
			pw.print("})");  //$NON-NLS-1$
		}
		pw.println();
	}

	private void printJoinColumnAnnotationsOn(ForeignKey foreignKey, EntitySourceWriter pw) {
		for (Iterator<ForeignKey.ColumnPair> stream = foreignKey.columnPairs(); stream.hasNext(); ) {
			this.printJoinColumnAnnotationOn(stream.next(), pw);
			if (stream.hasNext()) {
				pw.println(',');
			}
		}
	}

	private void printJoinColumnAnnotationOn(ForeignKey.ColumnPair columnPair, EntitySourceWriter pw) {
		this.printJoinColumnAnnotationOn(
			this.config.getDatabaseAnnotationNameBuilder().buildJoinColumnAnnotationName(columnPair.getBaseColumn()),
			this.config.getDatabaseAnnotationNameBuilder().buildJoinColumnAnnotationName(columnPair.getReferencedColumn()),
			pw
		);
	}

	/**
	 * 'baseColumnName' cannot be null;
	 * 'referencedColumnName' is null when the default is applicable (i.e. the
	 * referenced column is the single-column primary key column of the
	 * referenced table)
	 */
	private void printJoinColumnAnnotationOn(String baseColumnName, String referencedColumnName, EntitySourceWriter pw) {
		pw.printAnnotation(JPA.JOIN_COLUMN);
		pw.print("(name=");  //$NON-NLS-1$
		pw.printStringLiteral(baseColumnName);

		if (referencedColumnName != null) {
			pw.print(", referencedColumnName=");  //$NON-NLS-1$
			pw.printStringLiteral(referencedColumnName);
		}

		pw.print(')');
	}


	// ********** one-to-many fields **********

	private void printEntityOneToManyFieldsOn(EntitySourceWriter pw) {
		for (Iterator<OneToManyRelation> stream = this.genTable.oneToManyRelations(); stream.hasNext(); ) {
			this.printEntityOneToManyFieldOn(stream.next(), pw);
		}
	}

	private void printEntityOneToManyFieldOn(OneToManyRelation relation, EntitySourceWriter pw) {
		String fieldName = this.genTable.getAttributeNameFor(relation);
		if (this.config.fieldAccessType()) {
			this.printOneToManyAnnotationOn(relation, pw);
		}
		this.printCollectionFieldOn(fieldName, this.fullyQualify(relation.getReferencedEntityName()), pw);
	}

	private void printOneToManyAnnotationOn(OneToManyRelation relation, EntitySourceWriter pw) {
		pw.printAnnotation(JPA.ONE_TO_MANY);
		pw.print("(mappedBy=\"");  //$NON-NLS-1$
		pw.print(relation.getMappedBy());
		pw.print("\")");  //$NON-NLS-1$
		pw.println();
	}


	// ********** owned many-to-many fields **********

	private void printEntityOwnedManyToManyFieldsOn(EntitySourceWriter pw) {
		for (Iterator<ManyToManyRelation>  stream = this.genTable.ownedManyToManyRelations(); stream.hasNext(); ) {
			this.printEntityOwnedManyToManyFieldOn(stream.next(), pw);
		}
	}

	private void printEntityOwnedManyToManyFieldOn(ManyToManyRelation relation, EntitySourceWriter pw) {
		String fieldName = this.genTable.getAttributeNameForOwned(relation);
		if (this.config.fieldAccessType()) {
			this.printOwnedManyToManyAnnotationOn(fieldName, relation, pw);
		}
		this.printCollectionFieldOn(fieldName, this.fullyQualify(relation.getNonOwningEntityName()), pw);
	}

	/**
	 * only print the JoinTable annotation if one or more of the
	 * [generated] elements is not defaulted:
	 *     name
	 *     joinColumns
	 *     inverseJoinColumns
	 * thus the need for the 'printJoinTableAnnotation' flag
	 */
	private void printOwnedManyToManyAnnotationOn(String attributeName, ManyToManyRelation relation, EntitySourceWriter pw) {
		pw.printAnnotation(JPA.MANY_TO_MANY);
		pw.println();
		BooleanReference printJoinTableAnnotation = new BooleanReference(true);

		if ( ! relation.joinTableNameIsDefault()) {  // db-only test - no need to delegate to platform?
			printJoinTableAnnotation.setFalse();
			pw.printAnnotation(JPA.JOIN_TABLE);
			pw.print("(name=");  //$NON-NLS-1$
			pw.printStringLiteral(this.config.getDatabaseAnnotationNameBuilder().buildJoinTableAnnotationName(relation.getJoinGenTable().getTable()));
		}

		this.printJoinTableJoinColumnAnnotationsOn(
				"joinColumns",  //$NON-NLS-1$
				attributeName,
				relation.getOwningForeignKey(),
				printJoinTableAnnotation,
				pw
		);

		this.printJoinTableJoinColumnAnnotationsOn(
				"inverseJoinColumns",  //$NON-NLS-1$
				relation.getNonOwningGenTable().getAttributeNameForNonOwned(relation),
				relation.getNonOwningForeignKey(),
				printJoinTableAnnotation,
				pw
		);

		if (printJoinTableAnnotation.isFalse()) {
			pw.print(')');
			pw.println();
		}
	}

	/**
	 * 'elementName' is either "joinColumns" or "inverseJoinColumns"
	 */
	private void printJoinTableJoinColumnAnnotationsOn(String elementName, String attributeName, ForeignKey foreignKey, BooleanReference printJoinTableAnnotation, EntitySourceWriter pw) {
		// we have to pre-calculate whether either 'name' and/or 'referencedColumnName'
		// is required because they are wrapped by the JoinTable annotation and we
		// need to print the JoinTable annotation first (if it hasn't already been printed)
		boolean printRef = ! foreignKey.referencesSingleColumnPrimaryKey();
		// if 'referencedColumnName' is required, 'name' is also required (i.e. it cannot be defaulted);
		// but we will calculate it later [1], since there could be multiple join columns
		String joinColumnName = (printRef) ?
					null  // 'joinColumnName' is not used
				:
					this.config.getDatabaseAnnotationNameBuilder().buildJoinColumnAnnotationName(attributeName, foreignKey);
		boolean printBase = (printRef || (joinColumnName != null));
		if (printBase || printRef) {
			if (printJoinTableAnnotation.isTrue()) {
				printJoinTableAnnotation.setFalse();
				pw.printAnnotation(JPA.JOIN_TABLE);
				pw.print('(');
			} else {
				pw.print(',');
			}
			pw.println();
			pw.indent();
				if (printRef) {
					// if 'printRef' is true, 'joinColumnName' will always be "IGNORED" (so we ignore it)
					this.printJoinTableJoinColumnAnnotationsOn(elementName, foreignKey, pw);  // [1]
				} else {
					// if the FK references a single-column PK, 'referencedColumnName' is not required
					if (printBase) {
						// there is only a single join column here (just not the default name)
						pw.print(elementName);
						pw.print('=');
						this.printJoinColumnAnnotationOn(joinColumnName, null, pw);
					} else {
						// no JoinColumn annotation needed: the default 'name' and 'referencedColumnName' work
					}
				}
			pw.undent();
		}
	}

	/**
	 * 'elementName' is either "joinColumns" or "inverseJoinColumns"
	 */
	private void printJoinTableJoinColumnAnnotationsOn(String elementName, ForeignKey foreignKey, EntitySourceWriter pw) {
		pw.print(elementName);
		pw.print('=');
		if (foreignKey.columnPairsSize() > 1) {
			pw.print('{');
			pw.println();
			pw.indent();
		}
		this.printJoinColumnAnnotationsOn(foreignKey, pw);
		if (foreignKey.columnPairsSize() > 1) {
			pw.undent();
			pw.println();
			pw.print('}');
			pw.println();
		}
	}


	// ********** non-owned many-to-many fields **********

	private void printEntityNonOwnedManyToManyFieldsOn(EntitySourceWriter pw) {
		for (Iterator<ManyToManyRelation> stream = this.genTable.nonOwnedManyToManyRelations(); stream.hasNext(); ) {
			this.printEntityNonOwnedManyToManyFieldOn(stream.next(), pw);
		}
	}

	private void printEntityNonOwnedManyToManyFieldOn(ManyToManyRelation relation, EntitySourceWriter pw) {
		String fieldName = this.genTable.getAttributeNameForNonOwned(relation);
		if (this.config.fieldAccessType()) {
			this.printNonOwnedManyToManyAnnotationOn(relation, pw);
		}
		this.printCollectionFieldOn(fieldName, this.fullyQualify(relation.getOwningEntityName()), pw);
	}

	private void printNonOwnedManyToManyAnnotationOn(ManyToManyRelation relation, EntitySourceWriter pw) {
		pw.printAnnotation(JPA.MANY_TO_MANY);
		pw.print("(mappedBy=\"");  //$NON-NLS-1$
		pw.print(relation.getMappedBy());
		pw.print("\")");  //$NON-NLS-1$
		pw.println();
	}


	// ********** misc **********

	private void printSerialVersionUIDFieldOn(EntitySourceWriter pw) {
		if (this.config.generateSerialVersionUID()) {
			pw.print("private static final long serialVersionUID = 1L;");  //$NON-NLS-1$
			pw.println();
		}
	}

	private void printZeroArgumentConstructorOn(String ctorName, String visibility, EntitySourceWriter pw) {
		if (this.config.generateDefaultConstructor()) {
			pw.printVisibility(visibility);
			pw.print(ctorName);
			pw.print("() {");  //$NON-NLS-1$
			pw.println();
			pw.indent();
				pw.println("super();");  //$NON-NLS-1$
			pw.undent();
			pw.print('}');
			pw.println();
			pw.println();
		}
	}


	// ********** primary key properties **********

	private void printEntityPrimaryKeyPropertiesOn(EntitySourceWriter pw) {
		if (this.primaryKeyClassIsRequired() && this.config.generateEmbeddedIdForCompoundPK()) {
			this.printEntityEmbeddedIdPrimaryKeyPropertyOn(pw);
		} else {
			this.printEntityReadOnlyPrimaryKeyPropertiesOn(pw);
			this.printEntityWritablePrimaryKeyPropertiesOn(pw);
		}
	}

	private void printEntityEmbeddedIdPrimaryKeyPropertyOn(EntitySourceWriter pw) {
		if (this.config.propertyAccessType()) {
			pw.printAnnotation(JPA.EMBEDDED_ID);
			pw.println();
		}
		this.printPropertyOn(this.genTable.getAttributeNameForEmbeddedId(), this.pkClassName, pw);
	}

	private void printEntityReadOnlyPrimaryKeyPropertiesOn(EntitySourceWriter pw) {
		this.printPrimaryKeyPropertiesOn(pw, true, true);  // true=read-only; true=print ID annotation on getters
	}

	private void printEntityWritablePrimaryKeyPropertiesOn(EntitySourceWriter pw) {
		this.printPrimaryKeyPropertiesOn(pw, false, true);  // false=writable; true=print ID annotation on getters
	}

	private void printPrimaryKeyPropertiesOn(EntitySourceWriter pw, boolean readOnly, boolean printIdAnnotation) {
		for (Iterator<Column> stream = this.primaryKeyColumns(readOnly); stream.hasNext(); ) {
			this.printPrimaryKeyPropertyOn(stream.next(), pw, readOnly, printIdAnnotation);
		}
	}

	// TODO if the property's type is java.util/sql.Date, it needs @Temporal(DATE)
	// TODO if the primary key is auto-generated, the property must be an integral type
	private void printPrimaryKeyPropertyOn(Column column, EntitySourceWriter pw, boolean readOnly, boolean printIdAnnotation) {
		String propertyName = this.genTable.getAttributeNameFor(column);
		if (this.config.propertyAccessType()) {
			if (printIdAnnotation) {
				pw.printAnnotation(JPA.ID);
				pw.println();
			}
			String columnName = this.config.getDatabaseAnnotationNameBuilder().buildColumnAnnotationName(propertyName, column);
			if (readOnly) {
				this.printReadOnlyColumnAnnotationOn(columnName, pw);
			} else {
				this.printColumnAnnotationOn(columnName, pw);
			}
		}
		this.printPropertyOn(propertyName, column.getPrimaryKeyJavaTypeDeclaration(), pw);
	}


	// ********** basic properties **********

	private void printEntityNonPrimaryKeyBasicPropertiesOn(EntitySourceWriter pw) {
		for (Iterator<Column> stream = this.genTable.nonPrimaryKeyBasicColumns(); stream.hasNext(); ) {
			this.printEntityNonPrimaryKeyBasicPropertyOn(stream.next(), pw);
		}
	}

	private void printEntityNonPrimaryKeyBasicPropertyOn(Column column, EntitySourceWriter pw) {
		String propertyName = this.genTable.getAttributeNameFor(column);
		if (this.config.propertyAccessType()) {
			String columnName = this.config.getDatabaseAnnotationNameBuilder().buildColumnAnnotationName(propertyName, column);
			this.printColumnAnnotationOn(columnName, pw);
		}
		this.printPropertyOn(propertyName, column.getJavaTypeDeclaration(), pw);
	}


	// ********** many-to-one properties **********

	private void printEntityManyToOnePropertiesOn(EntitySourceWriter pw) {
		for (Iterator<ManyToOneRelation> stream = this.genTable.manyToOneRelations(); stream.hasNext(); ) {
			this.printEntityManyToOnePropertyOn(stream.next(), pw);
		}
	}

	private void printEntityManyToOnePropertyOn(ManyToOneRelation relation, EntitySourceWriter pw) {
		String propertyName = this.genTable.getAttributeNameFor(relation);
		if (this.config.propertyAccessType()) {
			this.printManyToOneAnnotationOn(propertyName, relation, pw);
		}
		String typeDeclaration = this.fullyQualify(relation.getReferencedEntityName());
		this.printPropertyOn(propertyName, typeDeclaration, pw);
	}


	// ********** one-to-many properties **********

	private void printEntityOneToManyPropertiesOn(EntitySourceWriter pw) {
		for (Iterator<OneToManyRelation> stream = this.genTable.oneToManyRelations(); stream.hasNext(); ) {
			this.printEntityOneToManyPropertyOn(stream.next(), pw);
		}
	}

	private void printEntityOneToManyPropertyOn(OneToManyRelation relation, EntitySourceWriter pw) {
		String propertyName = this.genTable.getAttributeNameFor(relation);
		if (this.config.propertyAccessType()) {
			this.printOneToManyAnnotationOn(relation, pw);
		}
		String elementTypeDeclaration = this.fullyQualify(relation.getReferencedEntityName());
		this.printCollectionPropertyOn(propertyName, elementTypeDeclaration, pw);
	}


	// ********** owned many-to-many properties **********

	private void printEntityOwnedManyToManyPropertiesOn(EntitySourceWriter pw) {
		for (Iterator<ManyToManyRelation> stream = this.genTable.ownedManyToManyRelations(); stream.hasNext(); ) {
			this.printEntityOwnedManyToManyPropertyOn(stream.next(), pw);
		}
	}

	private void printEntityOwnedManyToManyPropertyOn(ManyToManyRelation relation, EntitySourceWriter pw) {
		String propertyName = this.genTable.getAttributeNameForOwned(relation);
		if (this.config.propertyAccessType()) {
			this.printOwnedManyToManyAnnotationOn(propertyName, relation, pw);
		}
		String elementTypeDeclaration = this.fullyQualify(relation.getNonOwningEntityName());
		this.printCollectionPropertyOn(propertyName, elementTypeDeclaration, pw);
	}


	// ********** non-owned many-to-many properties **********

	private void printEntityNonOwnedManyToManyPropertiesOn(EntitySourceWriter pw) {
		for (Iterator<ManyToManyRelation> stream = this.genTable.nonOwnedManyToManyRelations(); stream.hasNext(); ) {
			this.printEntityNonOwnedManyToManyPropertyOn(stream.next(), pw);
		}
	}

	private void printEntityNonOwnedManyToManyPropertyOn(ManyToManyRelation relation, EntitySourceWriter pw) {
		String propertyName = this.genTable.getAttributeNameForNonOwned(relation);
		if (this.config.propertyAccessType()) {
			this.printNonOwnedManyToManyAnnotationOn(relation, pw);
		}
		String elementTypeDeclaration = this.fullyQualify(relation.getOwningEntityName());
		this.printCollectionPropertyOn(propertyName, elementTypeDeclaration, pw);
	}


	// ********** compound primary key class **********

	private void printPrimaryKeyClassOn(EntitySourceWriter pw) {
		pw.println();
		if (this.config.generateEmbeddedIdForCompoundPK()) {
			pw.printAnnotation(JPA.EMBEDDABLE);
			pw.println();
		}
		pw.print("public static class ");  //$NON-NLS-1$
		pw.print(this.config.getPrimaryKeyMemberClassName());
		pw.print(" implements ");  //$NON-NLS-1$
		pw.printTypeDeclaration(Serializable.class.getName());
		pw.print(" {");  //$NON-NLS-1$
		pw.println();

		pw.indent();
			if (this.config.generateEmbeddedIdForCompoundPK()) {
				this.printEmbeddableReadOnlyPrimaryKeyFieldsOn(pw);
				this.printEmbeddableWritablePrimaryKeyFieldsOn(pw);
			} else {
				this.printIdFieldsOn(pw);
			}
			this.printSerialVersionUIDFieldOn(pw);
			pw.println();
			this.printZeroArgumentConstructorOn(this.config.getPrimaryKeyMemberClassName(), "public", pw);  //$NON-NLS-1$

			if (this.config.propertyAccessType() || this.config.generateGettersAndSetters()) {
				if (this.config.generateEmbeddedIdForCompoundPK()) {
					this.printEmbeddableReadOnlyPrimaryKeyPropertiesOn(pw);
					this.printEmbeddableWritablePrimaryKeyPropertiesOn(pw);
				} else {
					this.printIdPropertiesOn(pw);
				}
			}

			this.printPrimaryKeyEqualsMethodOn(this.config.getPrimaryKeyMemberClassName(), this.getTable().primaryKeyColumns(), pw);
			this.printPrimaryKeyHashCodeMethodOn(this.getTable().primaryKeyColumns(), pw);
		pw.undent();

		pw.print('}');
		pw.println();
		pw.println();
	}


	// ********** compound primary key class fields **********

	private void printEmbeddableReadOnlyPrimaryKeyFieldsOn(EntitySourceWriter pw) {
		this.printPrimaryKeyFieldsOn(pw, true, false);  // true=read-only; false=do not print ID annotation on fields
	}

	private void printEmbeddableWritablePrimaryKeyFieldsOn(EntitySourceWriter pw) {
		this.printPrimaryKeyFieldsOn(pw, false, false);  // false=writable; false=do not print ID annotation on fields
	}

	private void printIdFieldsOn(EntitySourceWriter pw) {
		for (Iterator<Column> stream = this.getTable().primaryKeyColumns(); stream.hasNext(); ) {
			this.printIdFieldOn(stream.next(), pw);
		}
	}

	private void printIdFieldOn(Column column, EntitySourceWriter pw) {
		this.printFieldOn(this.genTable.getAttributeNameFor(column), column.getPrimaryKeyJavaTypeDeclaration(), pw);
	}


	// ********** compound primary key class properties **********

	private void printEmbeddableReadOnlyPrimaryKeyPropertiesOn(EntitySourceWriter pw) {
		this.printPrimaryKeyPropertiesOn(pw, true, false);  // true=read-only; false=do not print ID annotation on getters
	}

	private void printEmbeddableWritablePrimaryKeyPropertiesOn(EntitySourceWriter pw) {
		this.printPrimaryKeyPropertiesOn(pw, false, false);  // false=writable; false=do not print ID annotation on getters
	}

	private void printIdPropertiesOn(EntitySourceWriter pw) {
		for (Iterator<Column> stream = this.getTable().primaryKeyColumns(); stream.hasNext(); ) {
			this.printIdPropertyOn(stream.next(), pw);
		}
	}

	private void printIdPropertyOn(Column column, EntitySourceWriter pw) {
		this.printPropertyOn(this.genTable.getAttributeNameFor(column), column.getPrimaryKeyJavaTypeDeclaration(), pw);
	}


	// ********** compound primary key class equals **********

	private void printPrimaryKeyEqualsMethodOn(String className, Iterator<Column> columns, EntitySourceWriter pw) {
		pw.printAnnotation("java.lang.Override");  //$NON-NLS-1$
		pw.println();

		pw.println("public boolean equals(Object o) {");  //$NON-NLS-1$
		pw.indent();
			pw.println("if (o == this) {");  //$NON-NLS-1$
			pw.indent();
				pw.println("return true;");  //$NON-NLS-1$
			pw.undent();
			pw.print('}');
			pw.println();

			pw.print("if ( ! (o instanceof ");  //$NON-NLS-1$
			pw.print(className);
			pw.print(")) {");  //$NON-NLS-1$
			pw.println();
			pw.indent();
				pw.println("return false;");  //$NON-NLS-1$
			pw.undent();
			pw.print('}');
			pw.println();

			pw.print(className);
			pw.print(" other = (");  //$NON-NLS-1$
			pw.print(className);
			pw.print(") o;");  //$NON-NLS-1$
			pw.println();

			pw.print("return ");  //$NON-NLS-1$
			pw.indent();
				while (columns.hasNext()) {
					this.printPrimaryKeyEqualsClauseOn(columns.next(), pw);
					if (columns.hasNext()) {
						pw.println();
						pw.print("&& ");  //$NON-NLS-1$
					}
				}
				pw.print(';');
				pw.println();
			pw.undent();
		pw.undent();
		pw.print('}');
		pw.println();
		pw.println();
	}

	private void printPrimaryKeyEqualsClauseOn(Column column, EntitySourceWriter pw) {
		String fieldName = this.genTable.getAttributeNameFor(column);
		JavaType javaType = column.getPrimaryKeyJavaType();
		if (javaType.isPrimitive()) {
			this.printPrimitiveEqualsClauseOn(fieldName, pw);
		} else {
			this.printReferenceEqualsClauseOn(fieldName, pw);
		}
	}

	private void printPrimitiveEqualsClauseOn(String fieldName, EntitySourceWriter pw) {
		pw.print("(this.");  //$NON-NLS-1$
		pw.print(fieldName);
		pw.print(" == other.");  //$NON-NLS-1$
		pw.print(fieldName);
		pw.print(')');
	}

	private void printReferenceEqualsClauseOn(String fieldName, EntitySourceWriter pw) {
		pw.print("this.");  //$NON-NLS-1$
		pw.print(fieldName);
		pw.print(".equals(other.");  //$NON-NLS-1$
		pw.print(fieldName);
		pw.print(')');
	}


	// ********** compound primary key class hash code **********

	private void printPrimaryKeyHashCodeMethodOn(Iterator<Column> columns, EntitySourceWriter pw) {
		pw.printAnnotation("java.lang.Override");  //$NON-NLS-1$
		pw.println();

		pw.println("public int hashCode() {");  //$NON-NLS-1$
		pw.indent();
			pw.println("final int prime = 31;");  //$NON-NLS-1$
			pw.println("int hash = 17;");  //$NON-NLS-1$
			while (columns.hasNext()) {
				pw.print("hash = hash * prime + ");  //$NON-NLS-1$
				this.printPrimaryKeyHashCodeClauseOn(columns.next(), pw);
				pw.print(';');
				pw.println();
			}
			pw.println("return hash;");  //$NON-NLS-1$
		pw.undent();
		pw.print('}');
		pw.println();
		pw.println();
	}

	private void printPrimaryKeyHashCodeClauseOn(Column column, EntitySourceWriter pw) {
		String fieldName = this.genTable.getAttributeNameFor(column);
		JavaType javaType = column.getPrimaryKeyJavaType();
		if (javaType.isPrimitive()) {
			this.printPrimitiveHashCodeClauseOn(javaType.getElementTypeName(), fieldName, pw);
		} else {
			this.printReferenceHashCodeClauseOn(fieldName, pw);
		}
	}

	private void printPrimitiveHashCodeClauseOn(String primitiveName, String fieldName, EntitySourceWriter pw) {
		if (primitiveName.equals("int")) {  //$NON-NLS-1$
			// this.value
			pw.print("this.");  //$NON-NLS-1$
			pw.print(fieldName);
		} else if (primitiveName.equals("short")  //$NON-NLS-1$
				|| primitiveName.equals("byte")  //$NON-NLS-1$
				|| primitiveName.equals("char")) {  //$NON-NLS-1$
			// ((int) this.value)  - explicit cast
			pw.print("((int) this.");  //$NON-NLS-1$
			pw.print(fieldName);
			pw.print(')');
		} else if (primitiveName.equals("long")) {  // cribbed from Long#hashCode()  //$NON-NLS-1$
			// ((int) (this.value ^ (this.value >>> 32)))
			pw.print("((int) (this.");  //$NON-NLS-1$
			pw.print(fieldName);
			pw.print(" ^ (this.");  //$NON-NLS-1$
			pw.print(fieldName);
			pw.print(" >>> 32)))");  //$NON-NLS-1$
		} else if (primitiveName.equals("float")) {  // cribbed from Float#hashCode()  //$NON-NLS-1$
			// java.lang.Float.floatToIntBits(this.value)
			pw.printTypeDeclaration("java.lang.Float");  //$NON-NLS-1$
			pw.print(".floatToIntBits(this.");  //$NON-NLS-1$
			pw.print(fieldName);
			pw.print(')');
		} else if (primitiveName.equals("double")) {  // cribbed from Double#hashCode()  //$NON-NLS-1$
			//	((int) (java.lang.Double.doubleToLongBits(this.value) ^ (java.lang.Double.doubleToLongBits(this.value) >>> 32)))
			pw.print("((int) (");  //$NON-NLS-1$
			pw.printTypeDeclaration("java.lang.Double");  //$NON-NLS-1$
			pw.print(".doubleToLongBits(this.");  //$NON-NLS-1$
			pw.print(fieldName);
			pw.print(") ^ (");  //$NON-NLS-1$
			pw.printTypeDeclaration("java.lang.Double");  //$NON-NLS-1$
			pw.print(".doubleToLongBits(this.");  //$NON-NLS-1$
			pw.print(fieldName);
			pw.print(") >>> 32)))");  //$NON-NLS-1$
		} else if (primitiveName.equals("boolean")) {  //$NON-NLS-1$
			// (this.value ? 1 : 0)
			pw.print("(this.");  //$NON-NLS-1$
			pw.print(fieldName);
			pw.print(" ? 1 : 0)");  //$NON-NLS-1$
		} else {
			throw new IllegalArgumentException(primitiveName);
		}
	}

	private void printReferenceHashCodeClauseOn(String fieldName, EntitySourceWriter pw) {
		pw.print("this.");  //$NON-NLS-1$
		pw.print(fieldName);
		pw.print(".hashCode()");  //$NON-NLS-1$
	}


	// ********** package and imports **********

	private void printPackageAndImportsOn(PrintWriter pw, BodySource bodySource) {
		if (this.getPackageName().length() != 0) {
			pw.print("package ");  //$NON-NLS-1$
			pw.print(this.getPackageName());
			pw.print(';');
			pw.println();
			pw.println();
		}

		for (Iterator<Map.Entry<String, String>> stream = bodySource.importEntries(); stream.hasNext(); ) {
			Map.Entry<String, String> entry = stream.next();
			pw.print("import ");  //$NON-NLS-1$
			pw.print(entry.getValue());  // package
			pw.print('.');
			pw.print(entry.getKey());  // short class name
			pw.print(';');
			pw.println();
		}
		pw.println();
	}


	// ********** fields **********

	/**
	 * visibility is set in the config
	 */
	private void printFieldOn(String fieldName, String typeDeclaration, EntitySourceWriter pw) {
		pw.printField(
				fieldName,
				typeDeclaration,
				this.config.getFieldVisibilityClause()
		);
	}

	/**
	 * visibility and collection type are set in the config
	 */
	private void printCollectionFieldOn(String fieldName, String elementTypeDeclaration, EntitySourceWriter pw) {
		pw.printParameterizedField(
				fieldName,
				this.config.getCollectionTypeName(),
				elementTypeDeclaration,
				this.config.getFieldVisibilityClause()
		);
	}


	// ********** properties **********

	/**
	 * visibility is set in the config
	 */
	private void printPropertyOn(String propertyName, String typeDeclaration, EntitySourceWriter pw) {
		pw.printGetterAndSetter(
				propertyName,
				typeDeclaration,
				this.config.getMethodVisibilityClause()
		);
	}

	/**
	 * visibility and collection type are set in the config
	 */
	private void printCollectionPropertyOn(String propertyName, String elementTypeDeclaration, EntitySourceWriter pw) {
		pw.printCollectionGetterAndSetter(
				propertyName,
				this.config.getCollectionTypeName(),
				elementTypeDeclaration,
				this.config.getMethodVisibilityClause()
		);
	}


	// ********** convenience methods **********

	private String getPackageName() {
		return this.packageFragment.getElementName();
	}

	private Table getTable() {
		return this.genTable.getTable();
	}

	private String getEntityName() {
		return this.genTable.getEntityName();
	}

	private boolean primaryKeyClassIsRequired() {
		return this.getTable().primaryKeyColumnsSize() > 1;
	}

	private String fullyQualify(String shortClassName) {
		String pkg = this.getPackageName();
		return (pkg.length() == 0) ? shortClassName : pkg + '.' + shortClassName;
	}

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.genTable.getName() + " => " + this.entityClassName);  //$NON-NLS-1$
	}


	// ********** source writer **********

	private interface BodySource {

		/**
		 * return a sorted set of map entries; the key is the short class name,
		 * the value is the package name
		 */
		Iterator<Map.Entry<String, String>> importEntries();

		/**
		 * return the body source code
		 */
		String getSource();

		/**
		 * return the length of the body source code
		 */
		int length();

	}

	/**
	 * Extend IndentingPrintWriter with some methods that facilitate building
	 * class source code.
	 */
	private static class EntitySourceWriter extends IndentingPrintWriter implements BodySource {
		final String packageName;
		final String className;
		// key = short class name; value = package name
		private final Map<String, String> imports = new HashMap<String, String>();

		EntitySourceWriter(String packageName, String className) {
			super(new StringWriter(20000));
			this.packageName = packageName;
			this.className = className;
		}

		/**
		 * Convert the specified string to a String Literal and print it,
		 * adding the surrounding double-quotes and escaping characters
		 * as necessary.
		 */
		void printStringLiteral(String string) {
			StringTools.convertToJavaStringLiteralOn(string, this);
		}

		void printVisibility(String visibilityModifier) {
			if (visibilityModifier.length() != 0) {
				this.print(visibilityModifier);
				this.print(' ');
			}
		}

		void printAnnotation(String annotationName) {
			this.print('@');
			this.printTypeDeclaration(annotationName);
		}

		void printTypeDeclaration(String typeDeclaration) {
			this.print(this.buildImportedTypeDeclaration(typeDeclaration));
		}

		/**
		 * Return the specified class's "imported" name.
		 * The class declaration must be of the form:
		 *     "int"
		 *     "int[]" (not "[I")
		 *     "java.lang.Object"
		 *     "java.lang.Object[]" (not "[Ljava.lang.Object;")
		 *     "java.util.Map.Entry" (not "java.util.Map$Entry")
		 *     "java.util.Map.Entry[][]" (not "[[Ljava.util.Map$Entry;")
		 */
		private String buildImportedTypeDeclaration(String typeDeclaration) {
			if (this.typeDeclarationIsMemberClass(typeDeclaration)) {
				// no need for an import, just return the partially-qualified name
				return this.buildMemberClassTypeDeclaration(typeDeclaration);
			}
			int last = typeDeclaration.lastIndexOf('.');
			String pkg = (last == -1) ? "" : typeDeclaration.substring(0, last);  //$NON-NLS-1$
			String shortTypeDeclaration = typeDeclaration.substring(last + 1);
			String shortElementTypeName = shortTypeDeclaration;
			while (shortElementTypeName.endsWith("[]")) {  //$NON-NLS-1$
				shortElementTypeName = shortElementTypeName.substring(0, shortElementTypeName.length() - 2);
			}
			String prev = this.imports.get(shortElementTypeName);
			if (prev == null) {
				// this is the first class with this short element type name
				this.imports.put(shortElementTypeName, pkg);
				return shortTypeDeclaration;
			}
			if (prev.equals(pkg)) {
				// this element type has already been imported
				return shortTypeDeclaration;
			}
			// another class with the same short element type name has been
			// previously imported, so this one must be used fully-qualified
			return typeDeclaration;
		}

		/**
		 * e.g. "foo.bar.Employee.PK" will return true
		 */
		private boolean typeDeclarationIsMemberClass(String typeDeclaration) {
			return (typeDeclaration.length() > this.className.length())
					&& typeDeclaration.startsWith(this.className)
					&& (typeDeclaration.charAt(this.className.length()) == '.');
		}

		/**
		 * e.g. "foo.bar.Employee.PK" will return "Employee.PK"
		 * this prevents collisions with other imported classes (e.g. "joo.jar.PK")
		 */
		private String buildMemberClassTypeDeclaration(String typeDeclaration) {
			int index = this.packageName.length();
			if (index != 0) {
				index++;  // bump past the '.'
			}
			return typeDeclaration.substring(index);
		}

		private Iterator<Map.Entry<String, String>> sortedImportEntries() {
			TreeSet<Map.Entry<String, String>> sortedImports = new TreeSet<Map.Entry<String, String>>(this.buildImportEntriesComparator());
			sortedImports.addAll(this.imports.entrySet());
			return sortedImports.iterator();
		}

		private Comparator<Map.Entry<String, String>> buildImportEntriesComparator() {
			return new Comparator<Map.Entry<String, String>>() {
				public int compare(Map.Entry<String, String> e1, Map.Entry<String, String> e2) {
					Collator collator = Collator.getInstance();
					int pkg = collator.compare(e1.getValue(), e2.getValue());
					return (pkg == 0) ? collator.compare(e1.getKey(), e2.getKey()) : pkg;
				}
			};
		}

		void printField(String fieldName, String typeDeclaration, String visibility) {
			this.printVisibility(visibility);
			this.printTypeDeclaration(typeDeclaration);
			this.print(' ');
			this.print(fieldName);
			this.print(';');
			this.println();
			this.println();
		}

		void printParameterizedField(String fieldName, String typeDeclaration, String parameterTypeDeclaration, String visibility) {
			this.printVisibility(visibility);
			this.printTypeDeclaration(typeDeclaration);
			this.print('<');
			this.printTypeDeclaration(parameterTypeDeclaration);
			this.print('>');
			this.print(' ');
			this.print(fieldName);
			this.print(';');
			this.println();
			this.println();
		}

		void printGetterAndSetter(String propertyName, String typeDeclaration, String visibility) {
			this.printGetter(propertyName, typeDeclaration, visibility);
			this.println();
			this.println();

			this.printSetter(propertyName, typeDeclaration, visibility);
			this.println();
			this.println();
		}

		private void printGetter(String propertyName, String typeDeclaration, String visibility) {
			this.printVisibility(visibility);
			this.printTypeDeclaration(typeDeclaration);
			this.print(' ');
			this.print(typeDeclaration.equals("boolean") ? "is" : "get");  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			this.print(StringTools.capitalize(propertyName));
			this.print("() {");  //$NON-NLS-1$
			this.println();

			this.indent();
				this.print("return this.");  //$NON-NLS-1$
				this.print(propertyName);
				this.print(';');
				this.println();
			this.undent();

			this.print('}');
		}

		private void printSetter(String propertyName, String typeDeclaration, String visibility) {
			this.printVisibility(visibility);
			this.print("void set");  //$NON-NLS-1$
			this.print(StringTools.capitalize(propertyName));
			this.print('(');
			this.printTypeDeclaration(typeDeclaration);
			this.print(' ');
			this.print(propertyName);
			this.print(") {");  //$NON-NLS-1$
			this.println();

			this.indent();
				this.print("this.");  //$NON-NLS-1$
				this.print(propertyName);
				this.print(" = ");  //$NON-NLS-1$
				this.print(propertyName);
				this.print(';');
				this.println();
			this.undent();

			this.print('}');
		}

		void printCollectionGetterAndSetter(String propertyName, String collectionTypeDeclaration, String elementTypeDeclaration, String visibility) {
			this.printCollectionGetter(propertyName, collectionTypeDeclaration, elementTypeDeclaration, visibility);
			this.println();
			this.println();

			this.printCollectionSetter(propertyName, collectionTypeDeclaration, elementTypeDeclaration, visibility);
			this.println();
			this.println();
		}

		private void printCollectionGetter(String propertyName, String collectionTypeDeclaration, String elementTypeDeclaration, String visibility) {
			this.printVisibility(visibility);
			this.printTypeDeclaration(collectionTypeDeclaration);
			this.print('<');
			this.printTypeDeclaration(elementTypeDeclaration);
			this.print("> get");  //$NON-NLS-1$
			this.print(StringTools.capitalize(propertyName));
			this.print("() {");  //$NON-NLS-1$
			this.println();

			this.indent();
				this.print("return this.");  //$NON-NLS-1$
				this.print(propertyName);
				this.print(';');
				this.println();
			this.undent();

			this.print('}');
		}

		private void printCollectionSetter(String propertyName, String collectionTypeDeclaration, String elementTypeDeclaration, String visibility) {
			this.printVisibility(visibility);
			this.print("void set");  //$NON-NLS-1$
			this.print(StringTools.capitalize(propertyName));
			this.print('(');
			this.printTypeDeclaration(collectionTypeDeclaration);
			this.print('<');
			this.printTypeDeclaration(elementTypeDeclaration);
			this.print('>');
			this.print(' ');
			this.print(propertyName);
			this.print(") {");  //$NON-NLS-1$
			this.println();

			this.indent();
				this.print("this.");  //$NON-NLS-1$
				this.print(propertyName);
				this.print(" = ");  //$NON-NLS-1$
				this.print(propertyName);
				this.print(';');
				this.println();
			this.undent();

			this.print('}');
		}


		// ********** BodySource implementation **********

		public Iterator<Map.Entry<String, String>> importEntries() {
			return new FilteringIterator<Map.Entry<String, String>, Map.Entry<String, String>>(this.sortedImportEntries()) {
				@Override
				protected boolean accept(Map.Entry<String, String> next) {
					String pkg = next.getValue();
					if (pkg.equals("")  //$NON-NLS-1$
							|| pkg.equals("java.lang")  //$NON-NLS-1$
							|| pkg.equals(EntitySourceWriter.this.packageName)) {
						return false;
					}
					return true;
				}
			};
		}

		public String getSource() {
			return this.out.toString();
		}

		public int length() {
			return ((StringWriter) this.out).getBuffer().length();
		}

	}


	// ********** config **********

	public static class Config {
		private boolean convertToJavaStyleIdentifiers = true;

		private boolean propertyAccessType = false;  // as opposed to "field"

		private String collectionTypeName = Set.class.getName();
		private String collectionAttributeNameSuffix = "Collection";  // e.g. "private Set<Foo> fooCollection"  //$NON-NLS-1$

		private int fieldVisibility = Modifier.PRIVATE;
		private int methodVisibility = Modifier.PUBLIC;

		private boolean generateGettersAndSetters = true;
		private boolean generateDefaultConstructor = true;

		private boolean serializable = true;
		private boolean generateSerialVersionUID = true;

		private boolean generateEmbeddedIdForCompoundPK = true;  // as opposed to IdClass
		private String embeddedIdAttributeName = "pk";  //$NON-NLS-1$
		private String primaryKeyMemberClassName = "PK";  //$NON-NLS-1$

		/**
		 * key = table
		 * value = entity name
		 */
		private HashMap<Table, String> tables = new HashMap<Table, String>();

		private DatabaseAnnotationNameBuilder databaseAnnotationNameBuilder = DatabaseAnnotationNameBuilder.Default.INSTANCE;

		private OverwriteConfirmer overwriteConfirmer = OverwriteConfirmer.Never.INSTANCE;

		public static final int PRIVATE = 0;
		public static final int PACKAGE = 1;
		public static final int PROTECTED = 2;
		public static final int PUBLIC = 3;


		public boolean convertToJavaStyleIdentifiers() {
			return this.convertToJavaStyleIdentifiers;
		}
		public void setConvertToJavaStyleIdentifiers(boolean convertToJavaStyleIdentifiers) {
			this.convertToJavaStyleIdentifiers = convertToJavaStyleIdentifiers;
		}

		public boolean propertyAccessType() {
			return this.propertyAccessType;
		}
		public void setPropertyAccessType(boolean propertyAccessType) {
			this.propertyAccessType = propertyAccessType;
		}

		public boolean fieldAccessType() {
			return ! this.propertyAccessType;
		}
		public void setFieldAccessType(boolean fieldAccessType) {
			this.propertyAccessType = ! fieldAccessType;
		}

		public String getCollectionTypeName() {
			return this.collectionTypeName;
		}
		public void setCollectionTypeName(String collectionTypeName) {
			this.checkRequiredString(collectionTypeName, "collection type name is required");  //$NON-NLS-1$
			this.collectionTypeName = collectionTypeName;
		}

		public String getCollectionAttributeNameSuffix() {
			return this.collectionAttributeNameSuffix;
		}
		public void setCollectionAttributeNameSuffix(String collectionAttributeNameSuffix) {
			this.collectionAttributeNameSuffix = collectionAttributeNameSuffix;
		}

		public int getFieldVisibility() {
			return this.fieldVisibility;
		}
		/** entity fields cannot be 'public' */
		public void setFieldVisibility(int fieldVisibility) {
			switch (fieldVisibility) {
				case PRIVATE:
				case PACKAGE:
				case PROTECTED:
					this.fieldVisibility = fieldVisibility;
					break;
				default:
					throw new IllegalArgumentException("invalid field visibility: " + fieldVisibility);  //$NON-NLS-1$
			}
		}
		String getFieldVisibilityClause() {
			switch (this.fieldVisibility) {
				case PRIVATE:
					return "private";  //$NON-NLS-1$
				case PACKAGE:
					return "";  //$NON-NLS-1$
				case PROTECTED:
					return "protected";  //$NON-NLS-1$
				default:
					throw new IllegalStateException("invalid field visibility: " + this.fieldVisibility);  //$NON-NLS-1$
			}
		}

		public int getMethodVisibility() {
			return this.methodVisibility;
		}
		/** entity properties must be 'public' or 'protected' */
		public void setMethodVisibility(int methodVisibility) {
			switch (methodVisibility) {
				case PROTECTED:
				case PUBLIC:
					this.methodVisibility = methodVisibility;
					break;
				default:
					throw new IllegalArgumentException("invalid method visibility: " + methodVisibility);  //$NON-NLS-1$
			}
		}
		String getMethodVisibilityClause() {
			switch (this.methodVisibility) {
				case PROTECTED:
					return "protected";  //$NON-NLS-1$
				case PUBLIC:
					return "public";  //$NON-NLS-1$
				default:
					throw new IllegalStateException("invalid method visibility: " + this.methodVisibility);  //$NON-NLS-1$
			}
		}

		public boolean generateGettersAndSetters() {
			return this.generateGettersAndSetters;
		}
		public void setGenerateGettersAndSetters(boolean generateGettersAndSetters) {
			this.generateGettersAndSetters = generateGettersAndSetters;
		}

		public boolean generateDefaultConstructor() {
			return this.generateDefaultConstructor;
		}
		public void setGenerateDefaultConstructor(boolean generateDefaultConstructor) {
			this.generateDefaultConstructor = generateDefaultConstructor;
		}

		public boolean serializable() {
			return this.serializable;
		}
		public void setSerializable(boolean serializable) {
			this.serializable = serializable;
		}

		public boolean generateSerialVersionUID() {
			return this.generateSerialVersionUID;
		}
		public void setGenerateSerialVersionUID(boolean generateSerialVersionUID) {
			this.generateSerialVersionUID = generateSerialVersionUID;
		}

		public boolean generateEmbeddedIdForCompoundPK() {
			return this.generateEmbeddedIdForCompoundPK;
		}
		public void setGenerateEmbeddedIdForCompoundPK(boolean generateEmbeddedIdForCompoundPK) {
			this.generateEmbeddedIdForCompoundPK = generateEmbeddedIdForCompoundPK;
		}

		public boolean generateIdClassForCompoundPK() {
			return ! this.generateEmbeddedIdForCompoundPK;
		}
		public void setGenerateIdClassForCompoundPK(boolean generateIdClassForCompoundPK) {
			this.generateEmbeddedIdForCompoundPK = ! generateIdClassForCompoundPK;
		}

		public String getEmbeddedIdAttributeName() {
			return this.embeddedIdAttributeName;
		}
		public void setEmbeddedIdAttributeName(String embeddedIdAttributeName) {
			this.checkRequiredString(embeddedIdAttributeName, "EmbeddedId attribute name is required");  //$NON-NLS-1$
			this.embeddedIdAttributeName = embeddedIdAttributeName;
		}

		public String getPrimaryKeyMemberClassName() {
			return this.primaryKeyMemberClassName;
		}
		public void setPrimaryKeyMemberClassName(String primaryKeyMemberClassName) {
			this.checkRequiredString(primaryKeyMemberClassName, "primary key member class name is required");  //$NON-NLS-1$
			this.primaryKeyMemberClassName = primaryKeyMemberClassName;
		}

		String getEntityName(Table table) {
			return this.tables.get(table);
		}
		Iterator<Table> tables() {
			return this.tables.keySet().iterator();
		}
		int tablesSize() {
			return this.tables.size();
		}
		public void addTable(Table table, String entityName) {
			if (table == null) {
				throw new NullPointerException("table is required");  //$NON-NLS-1$
			}
			this.checkRequiredString(entityName, "entity name is required");  //$NON-NLS-1$
			if (this.tables.containsKey(table)) {
				throw new IllegalArgumentException("duplicate table: " + table.getName());  //$NON-NLS-1$
			}
			if (this.tables.values().contains(entityName)) {
				throw new IllegalArgumentException("duplicate entity name: " + entityName);  //$NON-NLS-1$
			}
			if ( ! NameTools.stringConsistsOfJavaIdentifierCharacters(entityName)) {
				throw new IllegalArgumentException("entity name is not a valid Java identifier: " + entityName);  //$NON-NLS-1$
			}
			if (NameTools.JAVA_RESERVED_WORDS_SET.contains(entityName)) {
				throw new IllegalArgumentException("entity name is a Java reserved word: " + entityName);  //$NON-NLS-1$
			}
			this.tables.put(table, entityName);
		}

		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;
		}

		public OverwriteConfirmer getOverwriteConfirmer() {
			return this.overwriteConfirmer;
		}
		public void setOverwriteConfirmer(OverwriteConfirmer overwriteConfirmer) {
			if (overwriteConfirmer == null) {
				throw new NullPointerException("overwrite confirmer is required");  //$NON-NLS-1$
			}
			this.overwriteConfirmer = overwriteConfirmer;
		}

		private void checkRequiredString(String string, String message) {
			if ((string == null) || (string.length() == 0)) {
				throw new IllegalArgumentException(message);
			}
		}

	}


	// ********** overwrite confirmer **********

	public static interface OverwriteConfirmer {
		/**
		 * Return whether the entity generator should overwrite the specified
		 * file.
		 */
		boolean overwrite(String className);


		final class Always implements OverwriteConfirmer {
			public static final OverwriteConfirmer INSTANCE = new Always();
			public static OverwriteConfirmer instance() {
				return INSTANCE;
			}
			// ensure single instance
			private Always() {
				super();
			}
			// everything will be overwritten
			public boolean overwrite(String arg0) {
				return true;
			}
			@Override
			public String toString() {
				return "OverwriteConfirmer.Always";  //$NON-NLS-1$
			}
		}


		final class Never implements OverwriteConfirmer {
			public static final OverwriteConfirmer INSTANCE = new Never();
			public static OverwriteConfirmer instance() {
				return INSTANCE;
			}
			// ensure single instance
			private Never() {
				super();
			}
			// nothing will be overwritten
			public boolean overwrite(String arg0) {
				return false;
			}
			@Override
			public String toString() {
				return "OverwriteConfirmer.Never";  //$NON-NLS-1$
			}
		}

	}


	// ********** annotation name builder **********

	/**
	 * Provide a pluggable way to determine whether and how the entity generator
	 * prints the names of various database objects.
	 */
	public static interface DatabaseAnnotationNameBuilder {

		/**
		 * Given the name of an entity and the table to which it is mapped,
		 * build and return a string to be used as the value for the entity's
		 * Table annotation's 'name' element. Return null if the entity
		 * maps to the table by default.
		 */
		String buildTableAnnotationName(String entityName, Table table);

		/**
		 * Given the name of an attribute (field or property) and the column
		 * to which it is mapped,
		 * build and return a string to be used as the value for the attribute's
		 * Column annotation's 'name' element. Return null if the attribute
		 * maps to the column by default.
		 */
		String buildColumnAnnotationName(String attributeName, Column column);

		/**
		 * Given the name of an attribute (field or property) and the
		 * many-to-one or many-to-many foreign key to which it is mapped,
		 * build and return a string to be used as the value for the attribute's
		 * JoinColumn annotation's 'name' element. Return null if the attribute
		 * maps to the join column by default.
		 * The specified foreign key consists of a single column pair whose
		 * referenced column is the single-column primary key of the foreign
		 * key's referenced table.
		 */
		String buildJoinColumnAnnotationName(String attributeName, ForeignKey foreignKey);

		/**
		 * Build and return a string to be used as the value for a JoinColumn
		 * annotation's 'name' or 'referencedColumnName' element.
		 * This is called for many-to-one and many-to-many mappings when
		 * the default join column name and/or referenced column name are/is
		 * not applicable.
		 * @see buildJoinColumnAnnotationName(String, ForeignKey)
		 */
		String buildJoinColumnAnnotationName(Column column);

		/**
		 * Build and return a string to be used as the value for a JoinTable
		 * annotation's 'name' element.
		 * This is called for many-to-many mappings when the default
		 * join table name is not applicable.
		 */
		String buildJoinTableAnnotationName(Table table);


		/**
		 * The default implementation simple returns the database object's name,
		 * unaltered.
		 */
		final class Default implements DatabaseAnnotationNameBuilder {
			public static final DatabaseAnnotationNameBuilder INSTANCE = new Default();
			public static DatabaseAnnotationNameBuilder instance() {
				return INSTANCE;
			}
			// ensure single instance
			private Default() {
				super();
			}
			public String buildTableAnnotationName(String entityName, Table table) {
				return table.getName();
			}
			public String buildColumnAnnotationName(String attributeName, Column column) {
				return column.getName();
			}
			public String buildJoinColumnAnnotationName(String attributeName, ForeignKey foreignKey) {
				return foreignKey.getColumnPair().getBaseColumn().getName();
			}
			public String buildJoinColumnAnnotationName(Column column) {
				return column.getName();
			}
			public String buildJoinTableAnnotationName(Table table) {
				return table.getName();
			}
			@Override
			public String toString() {
				return "DatabaseAnnotationNameBuilder.Default";  //$NON-NLS-1$
			}
		}

	}

}

Back to the top