给出代码示例:
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(javaTypeDetails);
...
FieldMetadataBuilder field = ...;
...
final AnnotationMetadataBuilder newAnnotationMetadata = new AnnotationMetadataBuilder(JpaJavaType.JOIN_TABLE);
newAnnotationMetadata.addStringAttribute("name", joinTableName);
List<NestedAnnotationAttributeValue> valueList;
AnnotationMetadataBuilder nestedAnnotationMetadata;
if (joinColumnName != null) {
valueList = new ArrayList<NestedAnnotationAttributeValue>(1);
nestedAnnotationMetadata = new AnnotationMetadataBuilder(JpaJavaType.JOIN_COLUMN);
nestedAnnotationMetadata.addStringAttribute("name", joinColumnName);
valueList.add(new NestedAnnotationAttributeValue(new JavaSymbolName("value"), nestedAnnotationMetadata.build()));
newAnnotationMetadata.addAttribute(new ArrayAttributeValue<NestedAnnotationAttributeValue>(
new JavaSymbolName("joinColumns"), valueList));
}
if (inverseJoinColumnName != null) {
valueList = new ArrayList<NestedAnnotationAttributeValue>(1);
nestedAnnotationMetadata = new AnnotationMetadataBuilder(JpaJavaType.JOIN_COLUMN);
nestedAnnotationMetadata.addStringAttribute("name", inverseJoinColumnName);
valueList.add(new NestedAnnotationAttributeValue(new JavaSymbolName("value"), nestedAnnotationMetadata.build()));
newAnnotationMetadata.addAttribute(new ArrayAttributeValue<NestedAnnotationAttributeValue>(
new JavaSymbolName("inverseJoinColumns"), valueList));
}
field.addAnnotation(newAnnotationMetadata.build());
这会在字段上产生无效的注释,例如:
@JoinTable(name = "NOTICE_EVENT_RECEIVER_X", joinColumns = { @javax.persistence.JoinColumn("NOTICE_EVENT_ID") }, inverseJoinColumns = { @javax.persistence.JoinColumn("NOTICE_RECEIVER_ID") })
如您所见,JoinColumn注释的属性“name”缺失,代码无法编译。 我做错了什么或者是Roo bug?
UPD:肯定存在 roo bug:当注释属性的数量为1时 - 假设它始终是“值”属性。这个假设是错误的!解决方法是再指定一个属性。例如(在我的场景中)“可空”。我这个案例生成的代码是可编译的。