@Option(name = "inverseFieldName",
required = false,
description = "Create an bi-directional relationship, using this value as the name of the inverse field.",
type = PromptType.JAVA_VARIABLE_NAME) final String inverseFieldName)
{
JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
try
{
JavaClass one = getJavaClass();
JavaClass many = findEntity(fieldType);
one.addImport(Set.class);
one.addImport(HashSet.class);
one.addImport(many.getQualifiedName());
Field<JavaClass> oneField = one.addField("private Set<" + many.getName() + "> " + fieldName + "= new HashSet<"
+ many.getName() + ">();");
Annotation<JavaClass> annotation = oneField.addAnnotation(OneToMany.class);
Refactory.createGetterAndSetter(one, oneField);
if ((inverseFieldName != null) && !inverseFieldName.isEmpty())
{
annotation.setStringValue("mappedBy", inverseFieldName);
annotation.setLiteralValue("cascade", "CascadeType.ALL");
annotation.getOrigin().addImport(CascadeType.class);
annotation.setLiteralValue("orphanRemoval", "true");
many.addImport(one);
Field<JavaClass> manyField = many.addField("private " + one.getName() + " " + inverseFieldName + ";");
manyField.addAnnotation(ManyToOne.class);
Refactory.createGetterAndSetter(many, manyField);
java.saveJavaSource(many);
}
java.saveJavaSource(one);
}
catch (FileNotFoundException e)
{
shell.println("Could not locate the @Entity requested. No update was made.");
}