}
@Override
public void onFinishNode(ProjectPath path) {
JpaEntity entity = path.firstInstanceOf(JpaEntity.class);
DataMap dataMap = targetPath.firstInstanceOf(DataMap.class);
ObjEntity cayenneEntity = targetPath.firstInstanceOf(ObjEntity.class);
// as superentity may not be loaded yet, must lookup DbEntity via JPA
// mapping...
DbEntity cayennePrimaryTable = dataMap.getDbEntity(entity
.lookupTable()
.getName());
for (JpaSecondaryTable secondaryTable : entity.getSecondaryTables()) {
// create a relationship between master DbEntity and a secondary
// DbEntity...
DbEntity cayenneSecondaryTable = dataMap.getDbEntity(secondaryTable
.getName());
JpaDbRelationship dbRelationship = new JpaDbRelationship(
getSecondaryTableDbRelationshipName(secondaryTable.getName()));
dbRelationship.setTargetEntityName(secondaryTable.getName());
cayennePrimaryTable.addRelationship(dbRelationship);
for (JpaPrimaryKeyJoinColumn column : secondaryTable
.getPrimaryKeyJoinColumns()) {
DbAttribute pkAttribute = (DbAttribute) cayennePrimaryTable
.getAttribute(column.getReferencedColumnName());
if (pkAttribute == null) {
recordConflict(path, "Invalid referenced column name: "
+ column.getReferencedColumnName());
continue;
}
DbAttribute attribute = new DbAttribute(column.getName());
attribute.setPrimaryKey(true);
attribute.setMandatory(true);
attribute.setAttributePrecision(pkAttribute.getAttributePrecision());
attribute.setType(pkAttribute.getType());
attribute.setMaxLength(pkAttribute.getMaxLength());
attribute.setAttributePrecision(pkAttribute.getAttributePrecision());
cayenneSecondaryTable.addAttribute(attribute);
DbJoin join = new DbJoin(dbRelationship, column
.getReferencedColumnName(), column.getName());
dbRelationship.addJoin(join);
}
dbRelationship.setToDependentPK(true);
dbRelationship.setToMany(false);
}
// init discriminator column
JpaDiscriminatorColumn discriminator = entity.lookupDiscriminatorColumn();
if (discriminator != null) {
if (cayennePrimaryTable.getAttribute(discriminator.getName()) == null) {
DbAttribute dbAttribute = new DbAttribute(discriminator.getName());
switch (discriminator.getDiscriminatorType()) {
case CHAR:
dbAttribute.setType(Types.CHAR);
dbAttribute.setMaxLength(1);
break;
case STRING:
dbAttribute.setType(Types.VARCHAR);
dbAttribute.setMaxLength(discriminator.getLength());
break;
case INTEGER:
dbAttribute.setType(Types.INTEGER);
break;
}
dbAttribute.setMandatory(false);
cayennePrimaryTable.addAttribute(dbAttribute);
}
String valueString = entity.getDiscriminatorValue();
if (valueString != null && valueString.length() > 0) {
Object value = null;
switch (discriminator.getDiscriminatorType()) {
case CHAR: