Skip to content

Commit

Permalink
implement ref table with real data
Browse files Browse the repository at this point in the history
  • Loading branch information
lilliCao committed Jul 17, 2020
1 parent b1651f8 commit 4863191
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public String getFirstEnumValue(String className) {
}
}

/**
/**
* Get the annotated table name of a given Entity class
*
* @param className
Expand Down Expand Up @@ -528,6 +528,62 @@ private static List<Field> getAllFields(List<Field> fields, Class<?> cl) {
return fields;
}

/**
* Helper method to get type of a field inclusive field from super classes
*
* @param pojoClass
* {@link Class} the class object of the pojo
* @param fieldName
* {@link String} the name of the field
* @return type of the field
*/
private Class<?> getTypeOfField(Class<?> pojoClass, String fieldName) {
if (pojoClass != null) {
List<Field> fields = new ArrayList();
getAllFields(fields, pojoClass);

Optional<Field> field = fields.stream().filter(f -> f.getName().equals(fieldName)).findFirst();

if (field.isPresent()) {
return field.get().getType();
}
}
LOG.error("Could not find type of field {}", fieldName);
return null;
}

/**
* @param pojoClass
* {@link Class} the class object of the pojo
* @param fieldName
* {@link String} the name of the field
* @return true if the field is an instance of java.utils.Collections
*/
public boolean isCollection2(Class<?> pojoClass, String fieldName) {
Class<?> type = getTypeOfField(pojoClass, fieldName);
return type == null ? false : Collection.class.isAssignableFrom(type);
}

/**
* @param className
* {@link String} full qualified class name
* @param fieldName
* {@link String} the name of the field
* @return type of the field in String
*/
public String getCanonicalNameOfField(String className, String fieldName) {
try {
Class<?> entityClass = Class.forName(className);
Class<?> type = getTypeOfField(entityClass, fieldName);
if (type != null) {
return type.getCanonicalName();
}
} catch (ClassNotFoundException e) {
LOG.error("{}: Could not find {}", e.getMessage(), className);
}
return null;
}

/**
* Get the primary key and its type of a given Entity class
*
Expand All @@ -548,9 +604,10 @@ public String getPrimaryKey(String className) {
} else {
Optional<Method> getterOptional = Arrays.stream(entityClass.getMethods())
.filter(m -> m.getName().equals(
"get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1)))
.findAny();
if (getterOptional.isPresent() && getterOptional.get().isAnnotationPresent(Id.class)) {
"get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1))
&& m.isAnnotationPresent(Id.class))
.findFirst();
if (getterOptional.isPresent()) {
Method getter = getterOptional.get();
return getter.getReturnType().getCanonicalName() + ","
+ (getter.isAnnotationPresent(Column.class) ? getter.getAnnotation(Column.class).name()
Expand All @@ -561,7 +618,7 @@ public String getPrimaryKey(String className) {
} catch (ClassNotFoundException e) {
LOG.error("{}: Could not find {}", e.getMessage(), className);
}
LOG.error("Could not found the field or getter with @Id annotated");
LOG.error("Could not find the field or getter with @Id annotated");
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<#include '/functions.ftl'>
<#if pojo.annotations.javax_persistence_Table?? && pojo.annotations.javax_persistence_Table.name??>
<#assign tableName = pojo.annotations.javax_persistence_Table.name>
<#else>
<#assign tableName = variables.entityName>
</#if>
<#assign tableName = JavaUtil.getEntityTableName(pojo.canonicalName)>
CREATE TABLE ${tableName} (
<#assign fkList = []>
<#assign columns = []>
Expand All @@ -12,8 +8,6 @@ CREATE TABLE ${tableName} (
<#if !field.annotations.javax_persistence_Transient??>
<#if field.annotations.javax_persistence_Column?? && field.annotations.javax_persistence_Column.name?has_content>
<#assign name = field.annotations.javax_persistence_Column.name>
<#elseif field.annotations.javax_persistence_JoinColumn?? && field.annotations.javax_persistence_JoinColumn.name?has_content>
<#assign name = field.annotations.javax_persistence_JoinColumn.name>
<#else>
<#assign name = field.name>
</#if>
Expand All @@ -38,7 +32,9 @@ CREATE TABLE ${tableName} (
<#assign type = get_mapping_type(pkReceived[0])>
<#assign id = pkReceived[1]>
</#if>
<#if name == field.name>
<#if field.annotations.javax_persistence_JoinColumn?? && field.annotations.javax_persistence_JoinColumn.name?has_content>
<#assign name = field.annotations.javax_persistence_JoinColumn.name>
<#else>
<#assign name = name + "_" + id>
</#if>
<#if field.annotations.javax_persistence_JoinColumn??
Expand All @@ -57,18 +53,26 @@ CREATE TABLE ${tableName} (
<#assign columns = columns + [{"name": name, "type":type}]>
<#else>
<#if field.annotations.javax_persistence_ManyToMany?? && field.annotations.javax_persistence_JoinTable??>
create new table
<#assign refTableName = "ref_table" refId1 = "id1" refType1 = "BIGINT" refId2 = "id2" refType2 = "BIGINT">
<#assign entity = field.canonicalType?substring(field.canonicalType?index_of("<") + 1,field.canonicalType?length - 1)>
<#if field.annotations.javax_persistence_JoinTable.name?has_content>
<#assign refTableName = field.annotations.javax_persistence_JoinTable.name>
<#else>
<#assign refTableName = tableName + "_" + JavaUtil.getEntityTableName(entity)>
</#if>
<#assign table1 = tableName>
<#if field.annotations.javax_persistence_JoinTable.joinColumns?has_content>
<#assign refId1 = field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn.name>
<#assign name1 = field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn.name>
<#if field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn.referencedColumnName?has_content>
<#assign id1 = field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn.referencedColumnName>
<#assign type1 = get_mapping_type(JavaUtil.getCanonicalNameOfField(pojo.canonicalName, id1))>
</#if>
<#else>
tbd
</#if>
<#if field.annotations.javax_persistence_JoinTable.inverseJoinColumns?has_content>
<#assign refId2 = field.annotations.javax_persistence_JoinTable.inverseJoinColumns[0].javax_persistence_JoinColumn.name>
</#if>
<#assign refTables = refTables + [{"table": refTableName, "id1": refId1, "type1": refType1, "id2": refId2, "type2": refType2}]>
<#assign refTables = refTables + [{"table": refTableName, "columns":[{"name": name1, "id": id1, "type": type1, "table": table1}, {"name": refId2, "id": "ID2", "type": "refType2", "table": "TB2"}] }]>
</#if>
</#if>
</#if>
Expand All @@ -84,8 +88,12 @@ CREATE TABLE ${tableName} (
<#list refTables as tb>

CREATE TABLE ${tb.table} (
${tb.id1?right_pad(30)} ${tb.type1} NOT NULL,
${tb.id2?right_pad(30)} ${tb.type2} NOT NULL,
CONSTRAINT PK_${tb.table} PRIMARY KEY(${tb.id1}, ${tb.id2}),
<#assign col1 = tb.columns[0]>
<#assign col2 = tb.columns[1]>
${col1.name?right_pad(30)} ${col1.type} NOT NULL,
${col2.name?right_pad(30)} ${col2.type} NOT NULL,
CONSTRAINT PK_${tb.table} PRIMARY KEY(${col1.name}, ${col2.name}),
CONSTRAINT FK_${tb.table}_${col1.name} FOREIGN KEY(${col1.name}) REFERENCES ${col1.table}(${col1.id}),
CONSTRAINT FK_${tb.table}_${col2.name} FOREIGN KEY(${col2.name}) REFERENCES ${col2.table}(${col2.id}),
);
</#list>

0 comments on commit 4863191

Please sign in to comment.