name = "package",
type = PromptType.JAVA_PACKAGE,
description = "The package name") final String packageName) throws Throwable
{
final PersistenceFacet jpa = project.getFacet(PersistenceFacet.class);
final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
String entityPackage;
if ((packageName != null) && !"".equals(packageName))
{
entityPackage = packageName;
}
else if (getPackagePortionOfCurrentDirectory() != null)
{
entityPackage = getPackagePortionOfCurrentDirectory();
}
else
{
entityPackage = shell.promptCommon(
"In which package you'd like to create this @Entity, or enter for default",
PromptType.JAVA_PACKAGE, jpa.getEntityPackage());
}
JavaClass javaClass = JavaParser.create(JavaClass.class)
.setPackage(entityPackage)
.setName(entityName)
.setPublic()
.addAnnotation(Entity.class).getOrigin()
.addInterface(Serializable.class);
Field<JavaClass> id = javaClass.addField("private Long id = null;");
id.addAnnotation(Id.class);
id.addAnnotation(GeneratedValue.class)
.setEnumValue("strategy", GenerationType.AUTO);
id.addAnnotation(Column.class)
.setStringValue("name", "id")
.setLiteralValue("updatable", "false")
.setLiteralValue("nullable", "false");
Field<JavaClass> version = javaClass.addField("private int version = 0;");
version.addAnnotation(Version.class);
version.addAnnotation(Column.class).setStringValue("name", "version");
Refactory.createGetterAndSetter(javaClass, id);
Refactory.createGetterAndSetter(javaClass, version);
Refactory.createToStringFromFields(javaClass, id);
Refactory.createHashCodeAndEquals(javaClass);
JavaResource javaFileLocation = java.saveJavaSource(javaClass);
shell.println("Created @Entity [" + javaClass.getQualifiedName() + "]");
/**
* Pick up the generated resource.