for (int j=0;j<constrs.length;j++)
{
UniqueMetaData unimd = new UniqueMetaData(null, (String)annotationValues.get("name"), null);
for (int k=0;k<constrs[j].columnNames().length;k++)
{
unimd.addColumn(new ColumnMetaData(unimd, constrs[j].columnNames()[k]));
}
uniques.add(unimd);
}
}
}
else if (annName.equals(JPAAnnotationUtils.ID_CLASS))
{
idClassName = ((Class)annotationValues.get("value")).getName();
}
else if (annName.equals(JPAAnnotationUtils.INHERITANCE))
{
// Only valid in the root class
InheritanceType inhType = (InheritanceType)annotationValues.get("strategy");
inheritanceStrategyForTree = inhType.toString();
if (inhType == InheritanceType.JOINED)
{
inheritanceStrategy = InheritanceStrategy.NEW_TABLE.toString();
}
else if (inhType == InheritanceType.TABLE_PER_CLASS)
{
inheritanceStrategy = InheritanceStrategy.COMPLETE_TABLE.toString();
}
else if (inhType == InheritanceType.SINGLE_TABLE)
{
// Translated to root class as "new-table" and children as "superclass-table"
// and @Inheritance should only be specified on root class so defaults to JPOX internal default
}
}
else if (annName.equals(JPAAnnotationUtils.DISCRIMINATOR_COLUMN))
{
discriminatorColumnName = (String)annotationValues.get("name");
DiscriminatorType type = (DiscriminatorType)annotationValues.get("discriminatorType");
if (type == DiscriminatorType.CHAR)
{
discriminatorColumnType = "CHAR";
}
else if (type == DiscriminatorType.INTEGER)
{
discriminatorColumnType = "INTEGER";
}
else if (type == DiscriminatorType.STRING)
{
discriminatorColumnType = "VARCHAR";
}
discriminatorColumnLength = (Integer)annotationValues.get("length");
// TODO Support JPA "columnDefinition"
}
else if (annName.equals(JPAAnnotationUtils.DISCRIMINATOR_VALUE))
{
discriminatorValue = (String)annotationValues.get("value");
}
else if (annName.equals(JPAAnnotationUtils.EMBEDDABLE))
{
embeddedOnly = "true";
persistenceModifier = ClassPersistenceModifier.PERSISTENCE_CAPABLE.toString();
identityType = org.jpox.metadata.IdentityType.NONDURABLE.toString();
}
else if (annName.equals(JPAAnnotationUtils.ENTITY_LISTENERS))
{
entityListeners = (Class[])annotationValues.get("value");
}
else if (annName.equals(JPAAnnotationUtils.EXCLUDE_SUPERCLASS_LISTENERS))
{
excludeSuperClassListeners = true;
}
else if (annName.equals(JPAAnnotationUtils.EXCLUDE_DEFAULT_LISTENERS))
{
excludeDefaultListeners = true;
}
else if (annName.equals(JPAAnnotationUtils.SEQUENCE_GENERATOR))
{
processSequenceGeneratorAnnotation(pmd, annotationValues);
}
else if (annName.equals(JPAAnnotationUtils.TABLE_GENERATOR))
{
processTableGeneratorAnnotation(pmd, annotationValues);
}
else if (annName.equals(JPAAnnotationUtils.PRIMARY_KEY_JOIN_COLUMN))
{
// Override the PK column name when we have a persistent superclass
pkColumnMetaData = new ColumnMetaData[1];
pkColumnMetaData[0] = new ColumnMetaData(cmd,
(String)annotationValues.get("name"), (String)annotationValues.get("referencedColumnName"),
null, null, null, null, null, null, null, null, null, null, null);
}
else if (annName.equals(JPAAnnotationUtils.PRIMARY_KEY_JOIN_COLUMNS))
{
// Override the PK column names when we have a persistent superclass
PrimaryKeyJoinColumn[] values = (PrimaryKeyJoinColumn[])annotationValues.get("value");
pkColumnMetaData = new ColumnMetaData[values.length];
for (int j=0;j<values.length;j++)
{
// TODO Support columnDefinition
pkColumnMetaData[j] = new ColumnMetaData(cmd,
values[j].name(), values[j].referencedColumnName(),
null, null, null, null, null, null, null, null, null, null, null);
}
}
else if (annName.equals(JPAAnnotationUtils.ATTRIBUTE_OVERRIDES))
{
AttributeOverride[] overrides = (AttributeOverride[])annotationValues.get("value");
if (overrides != null)
{
if (overriddenFields == null)
{
overriddenFields = new HashSet<AbstractMemberMetaData>();
}
for (int j=0;j<overrides.length;j++)
{
AbstractMemberMetaData fmd = mgr.getMetaDataFactory().newFieldObject(cmd,
"#UNKNOWN." + overrides[j].name(),
null, "persistent", null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null);
Column col = overrides[j].column();
// TODO Make inferrals about jdbctype, length etc if the field is 1 char etc
fmd.addColumn(new ColumnMetaData(fmd, col.name(), null, null, null, null,
"" + col.length(), "" + col.scale(), "" + col.nullable(), null, null,
"" + col.insertable(), "" + col.updatable(), "" + col.unique()));
overriddenFields.add(fmd);
}
}
}
else if (annName.equals(JPAAnnotationUtils.ATTRIBUTE_OVERRIDE))
{
if (overriddenFields == null)
{
overriddenFields = new HashSet<AbstractMemberMetaData>();
}
AbstractMemberMetaData fmd = mgr.getMetaDataFactory().newFieldObject(cmd,
"#UNKNOWN." + (String)annotationValues.get("name"),
null, null, null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null);
Column col = (Column)annotationValues.get("column");
// TODO Make inferrals about jdbctype, length etc if the field is 1 char etc
fmd.addColumn(new ColumnMetaData(fmd, col.name(), null, null, null, null,
"" + col.length(), "" + col.scale(), "" + col.nullable(), null, null,
"" + col.insertable(), "" + col.updatable(), "" + col.unique()));
overriddenFields.add(fmd);
}
else if (annName.equals(JPAAnnotationUtils.ASSOCIATION_OVERRIDES))
{
AssociationOverride[] overrides = (AssociationOverride[])annotationValues.get("value");
if (overrides != null)
{
if (overriddenFields == null)
{
overriddenFields = new HashSet<AbstractMemberMetaData>();
}
for (int j=0;j<overrides.length;j++)
{
AbstractMemberMetaData fmd = mgr.getMetaDataFactory().newFieldObject(cmd,
"#UNKNOWN." + overrides[j].name(),
null, null, null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null);
JoinColumn[] cols = overrides[j].joinColumns();
for (int k=0;k<cols.length;k++)
{
fmd.addColumn(new ColumnMetaData(fmd, cols[k].name(), null,
cols[k].referencedColumnName(), null, null,
null, null, "" + cols[k].nullable(), null, null,
"" + cols[k].insertable(), "" + cols[k].updatable(), "" + cols[k].unique()));
}
overriddenFields.add(fmd);
}
}
}
else if (annName.equals(JPAAnnotationUtils.ASSOCIATION_OVERRIDE))
{
if (overriddenFields == null)
{
overriddenFields = new HashSet<AbstractMemberMetaData>();
}
AbstractMemberMetaData fmd = mgr.getMetaDataFactory().newFieldObject(cmd,
"#UNKNOWN." + (String)annotationValues.get("name"),
null, null, null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null);
JoinColumn[] cols = (JoinColumn[])annotationValues.get("joinColumns");
for (int k=0;k<cols.length;k++)
{
fmd.addColumn(new ColumnMetaData(fmd, cols[k].name(), null,
cols[k].referencedColumnName(), null, null,
null, null, "" + cols[k].nullable(), null, null,
"" + cols[k].insertable(), "" + cols[k].updatable(), "" + cols[k].unique()));
}
overriddenFields.add(fmd);
}
else if (annName.equals(JPAAnnotationUtils.NAMED_QUERIES))
{
NamedQuery[] queries = (NamedQuery[])annotationValues.get("value");
if (namedQueries == null)
{
namedQueries = new HashSet<QueryMetaData>();
}
for (int j=0;j<queries.length;j++)
{
QueryMetaData qmd = new QueryMetaData(cmd, null, queries[j].name(), QueryLanguage.JPQL.toString(),
"true", null, null, null, null);
qmd.setQuery(queries[j].query());
namedQueries.add(qmd);
}
}
else if (annName.equals(JPAAnnotationUtils.NAMED_QUERY))
{
if (namedQueries == null)
{
namedQueries = new HashSet<QueryMetaData>();
}
QueryMetaData qmd = new QueryMetaData(cmd, null, (String)annotationValues.get("name"),
QueryLanguage.JPQL.toString(), "true", null, null, null, null);
qmd.setQuery((String)annotationValues.get("query"));
namedQueries.add(qmd);
}
else if (annName.equals(JPAAnnotationUtils.NAMED_NATIVE_QUERIES))
{
NamedNativeQuery[] queries = (NamedNativeQuery[])annotationValues.get("value");
if (namedQueries == null)
{
namedQueries = new HashSet<QueryMetaData>();
}
for (int j=0;j<queries.length;j++)
{
String resultClassName = null;
if (queries[j].resultClass() != null && queries[j].resultClass() != void.class)
{
resultClassName = queries[j].resultClass().getName();
}
String resultMappingName = null;
if (queries[j].resultSetMapping() != null)
{
resultMappingName = queries[j].resultSetMapping();
}
QueryMetaData qmd = new QueryMetaData(cmd, null, queries[j].name(), QueryLanguage.SQL.toString(),
"true", resultClassName, resultMappingName, null, null);
qmd.setQuery(queries[j].query());
namedQueries.add(qmd);
}
}
else if (annName.equals(JPAAnnotationUtils.NAMED_NATIVE_QUERY))
{
if (namedQueries == null)
{
namedQueries = new HashSet<QueryMetaData>();
}
Class resultClass = (Class)annotationValues.get("resultClass");
String resultClassName = null;
if (resultClass != null && resultClass != void.class)
{
resultClassName = resultClass.getName();
}
String resultMappingName = (String)annotationValues.get("resultSetMapping");
if (StringUtils.isWhitespace(resultMappingName))
{
resultMappingName = null;
}
QueryMetaData qmd = new QueryMetaData(cmd, null, (String)annotationValues.get("name"),
QueryLanguage.SQL.toString(), "true", resultClassName, resultMappingName, null, null);
qmd.setQuery((String)annotationValues.get("query"));
namedQueries.add(qmd);
}
else if (annName.equals(JPAAnnotationUtils.SQL_RESULTSET_MAPPINGS))
{
SqlResultSetMapping[] mappings = (SqlResultSetMapping[])annotationValues.get("value");
if (resultMappings == null)
{
resultMappings = new ArrayList<QueryResultMetaData>();
}
for (int j=0;j<mappings.length;j++)
{
QueryResultMetaData qrmd = new QueryResultMetaData(cmd, mappings[j].name());
EntityResult[] entityResults = (EntityResult[])mappings[j].entities();
if (entityResults != null)
{
for (int k=0;k<entityResults.length;k++)
{
String entityClassName = entityResults[k].entityClass().getName();
qrmd.addPersistentTypeMapping(entityClassName, null,
entityResults[k].discriminatorColumn());
FieldResult[] fields = entityResults[k].fields();
if (fields != null)
{
for (int l=0;l<fields.length;l++)
{
qrmd.addMappingForPersistentTypeMapping(entityClassName, fields[l].name(), fields[l].column());
}
}
}
}
ColumnResult[] colResults = (ColumnResult[])mappings[j].columns();
if (colResults != null)
{
for (int k=0;k<colResults.length;k++)
{
qrmd.addScalarColumn(colResults[k].name());
}
}
resultMappings.add(qrmd);
}
}
else if (annName.equals(JPAAnnotationUtils.SQL_RESULTSET_MAPPING))
{
if (resultMappings == null)
{
resultMappings = new ArrayList<QueryResultMetaData>();
}
QueryResultMetaData qrmd = new QueryResultMetaData(cmd, (String)annotationValues.get("name"));
EntityResult[] entityResults = (EntityResult[])annotationValues.get("entities");
if (entityResults != null)
{
for (int j=0;j<entityResults.length;j++)
{
String entityClassName = entityResults[j].entityClass().getName();
qrmd.addPersistentTypeMapping(entityClassName, null,
entityResults[j].discriminatorColumn());
FieldResult[] fields = entityResults[j].fields();
if (fields != null)
{
for (int k=0;k<fields.length;k++)
{
qrmd.addMappingForPersistentTypeMapping(entityClassName, fields[k].name(), fields[k].column());
}
}
}
}
ColumnResult[] colResults = (ColumnResult[])annotationValues.get("columns");
if (colResults != null)
{
for (int j=0;j<colResults.length;j++)
{
qrmd.addScalarColumn(colResults[j].name());
}
}
resultMappings.add(qrmd);
}
else if (annName.equals(JPAAnnotationUtils.SECONDARY_TABLES))
{
// processed below in newJoinMetaData
}
else if (annName.equals(JPAAnnotationUtils.SECONDARY_TABLE))
{
// processed below in newJoinMetaData
}
else if (annName.equals(JPAAnnotationUtils.EXTENSION))
{
// JPOX extension
ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
(String)annotationValues.get("key"), (String)annotationValues.get("value"));
extensions = new HashSet<ExtensionMetaData>(1);
extensions.add(extmd);
}
else if (annName.equals(JPAAnnotationUtils.EXTENSIONS))
{
// JPOX extension
Extension[] values = (Extension[])annotationValues.get("value");
if (values != null && values.length > 0)
{
extensions = new HashSet<ExtensionMetaData>(values.length);
for (int j=0;j<values.length;j++)
{
ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
values[j].key().toString(), values[j].value().toString());
extensions.add(extmd);
}
}
}
else
{
JPOXLogger.METADATA.error(LOCALISER.msg("MetaData.Annotations.AnnotationNotProcessed",
cls.getName(), annotations[i].getName()));
}
}
}
if (entityName == null || entityName.length() == 0)
{
entityName = ClassUtils.getClassNameForClass(cls);
}
if (persistenceModifier.equals(ClassPersistenceModifier.PERSISTENCE_CAPABLE.toString()) ||
persistenceModifier.equals(ClassPersistenceModifier.PERSISTENCE_AWARE.toString()))
{
JPOXLogger.METADATA.info(LOCALISER.msg("MetaData.Annotations.ClassUsingAnnotations", cls.getName(), "JPA"));
if (pmd == null)
{
// No owning package defined so add one
FileMetaData filemd = new FileMetaData("annotations", null, null, null);
filemd.setType(FileMetaData.ANNOTATIONS);
pmd = new PackageMetaData(filemd, cls.getPackage().getName(), null, null);
filemd.addPackage(pmd);
}
boolean superClassPC = isClassPersistenceCapable(cls.getSuperclass());
cmd = mgr.getMetaDataFactory().newClassObject(pmd, ClassUtils.getClassNameForClass(cls), identityType, idClassName,
requiresExtent, detachable, embeddedOnly, persistenceModifier,
superClassPC ? cls.getSuperclass().getName() : null, catalog, schema, table, entityName);
if (excludeSuperClassListeners)
{
cmd.excludeSuperClassListeners();
}
if (excludeDefaultListeners)
{
cmd.excludeDefaultListeners();
}
if (entityListeners != null)
{
for (int i=0; i<entityListeners.length; i++)
{
// Any EventListener will not have their callback methods registered at this point
EventListenerMetaData elmd = new EventListenerMetaData(entityListeners[i].getName());
cmd.addListener(elmd);
}
}
pmd.addClass(cmd);
// Inheritance
InheritanceMetaData inhmd = null;
if (inheritanceStrategy != null)
{
// Strategy specified so create inheritance data
inhmd = new InheritanceMetaData(cmd, inheritanceStrategy);
inhmd.setStrategyForTree(inheritanceStrategyForTree);
}
else if (discriminatorValue != null || discriminatorColumnName != null ||
discriminatorColumnLength != null || discriminatorColumnType != null)
{
// Discriminator specified so we need inheritance data
inhmd = new InheritanceMetaData(cmd, null);
inhmd.setStrategyForTree(inheritanceStrategyForTree);
}
if (discriminatorValue != null || discriminatorColumnName != null ||
discriminatorColumnLength != null || discriminatorColumnType != null)
{
// Add discriminator information to the inheritance of this class
DiscriminatorMetaData dismd = null;
if (discriminatorColumnType != null && discriminatorColumnType != "VARCHAR")
{
dismd = new DiscriminatorMetaData(inhmd, discriminatorColumnName, discriminatorValue,
"value-map", "false");
}
else
{
dismd = new DiscriminatorMetaData(inhmd, discriminatorColumnName, discriminatorValue,
"class-name", "false");
}
ColumnMetaData discolmd = null;
if (discriminatorColumnLength != null || discriminatorColumnName != null || discriminatorColumnType != null)
{
discolmd = new ColumnMetaData(dismd, discriminatorColumnName);
if (discriminatorColumnType != null)
{
discolmd.setJdbcType(discriminatorColumnType);
}
if (discriminatorColumnLength != null)
{
discolmd.setLength(discriminatorColumnLength);
}
dismd.setColumnMetaData(discolmd);
}
inhmd.setDiscriminatorMetaData(dismd);
}