return getIdentifierField(embeddedIdFields, EMBEDDED_ID);
}
// Ensure there isn't already a field called "id"; if so, compute a
// unique name (it's not really a fatal situation at the end of the day)
final JavaSymbolName idField = governorTypeDetails
.getUniqueFieldName(getIdentifierFieldName());
// We need to create one
final JavaType identifierType = getIdentifierType();
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final boolean hasIdClass = !(identifierType.isCoreType() || identifierType
.equals(GAE_DATASTORE_KEY));
final JavaType annotationType = hasIdClass ? EMBEDDED_ID : ID;
annotations.add(new AnnotationMetadataBuilder(annotationType));
// Encode keys as strings on GAE to support entity group hierarchies
if (isGaeEnabled && identifierType.equals(JavaType.STRING)) {
AnnotationMetadataBuilder extensionBuilder = new AnnotationMetadataBuilder(
DATANUCLEUS_JPA_EXTENSION);
extensionBuilder.addStringAttribute("vendorName", "datanucleus");
extensionBuilder.addStringAttribute("key", "gae.encoded-pk");
extensionBuilder.addStringAttribute("value", "true");
annotations.add(extensionBuilder);
}
// Compute the column name, as required
if (!hasIdClass) {
if (!"".equals(annotationValues.getSequenceName())) {
String generationType = isGaeEnabled || isDatabaseDotComEnabled ? "IDENTITY"
: "AUTO";
// ROO-746: Use @GeneratedValue(strategy = GenerationType.TABLE)
// If the root of the governor declares @Inheritance(strategy =
// InheritanceType.TABLE_PER_CLASS)
if ("AUTO".equals(generationType)) {
AnnotationMetadata inheritance = governorTypeDetails
.getAnnotation(INHERITANCE);
if (inheritance == null) {
inheritance = getInheritanceAnnotation();
}
if (inheritance != null) {
final AnnotationAttributeValue<?> value = inheritance
.getAttribute(new JavaSymbolName("strategy"));
if (value instanceof EnumAttributeValue) {
final EnumAttributeValue enumAttributeValue = (EnumAttributeValue) value;
final EnumDetails details = enumAttributeValue
.getValue();
if (details != null
&& details.getType().equals(
INHERITANCE_TYPE)) {
if ("TABLE_PER_CLASS".equals(details.getField()
.getSymbolName())) {
generationType = "TABLE";
}
}
}
}
}
final AnnotationMetadataBuilder generatedValueBuilder = new AnnotationMetadataBuilder(
GENERATED_VALUE);
generatedValueBuilder.addEnumAttribute("strategy",
new EnumDetails(GENERATION_TYPE, new JavaSymbolName(
generationType)));
if (StringUtils.isNotBlank(annotationValues.getSequenceName())
&& !(isGaeEnabled || isDatabaseDotComEnabled)) {
final String sequenceKey = StringUtils
.uncapitalize(destination.getSimpleTypeName())
+ "Gen";
generatedValueBuilder.addStringAttribute("generator",
sequenceKey);
final AnnotationMetadataBuilder sequenceGeneratorBuilder = new AnnotationMetadataBuilder(
SEQUENCE_GENERATOR);
sequenceGeneratorBuilder.addStringAttribute("name",
sequenceKey);
sequenceGeneratorBuilder.addStringAttribute("sequenceName",
annotationValues.getSequenceName());
annotations.add(sequenceGeneratorBuilder);
}
annotations.add(generatedValueBuilder);
}
final String identifierColumn = StringUtils
.stripToEmpty(getIdentifierColumn());
String columnName = idField.getSymbolName();
if (StringUtils.isNotBlank(identifierColumn)) {
// User has specified an alternate column name
columnName = identifierColumn;
}