diff --git a/cobigen-templates/templates-devon4j/src/main/templates/sql_trigger/templates/V0000__Create_${variables.entityName}.sql.ftl b/cobigen-templates/templates-devon4j/src/main/templates/sql_trigger/templates/V0000__Create_${variables.entityName}.sql.ftl index f43d89d6d9..13b7dcaf97 100644 --- a/cobigen-templates/templates-devon4j/src/main/templates/sql_trigger/templates/V0000__Create_${variables.entityName}.sql.ftl +++ b/cobigen-templates/templates-devon4j/src/main/templates/sql_trigger/templates/V0000__Create_${variables.entityName}.sql.ftl @@ -11,6 +11,7 @@ CREATE TABLE ${tableName} ( <#else> <#assign name = field.name> + <#--Field: primary key--> <#if field.annotations.javax_persistence_Id??> <#assign pk = name> <#assign type = get_type(field)> @@ -23,6 +24,7 @@ CREATE TABLE ${tableName} ( <#assign columns = columns + [{"name": name, "type":type}]> <#elseif !JavaUtil.isCollection2(classObject, field.name)> + <#--Field: simple entity--> <#if field.type?ends_with("Entity")> <#if field.annotations.javax_persistence_JoinColumn?? && field.annotations.javax_persistence_JoinColumn.referencedColumnName?has_content> <#assign id = field.annotations.javax_persistence_JoinColumn.referencedColumnName> @@ -48,31 +50,56 @@ CREATE TABLE ${tableName} ( <#assign fkList = fkList + [{"key": name, "table": table, "id": id}]> <#else> + <#--Field: primitive--> <#assign type = get_type(field)/> <#assign columns = columns + [{"name": name, "type":type}]> <#else> <#if field.annotations.javax_persistence_ManyToMany?? && field.annotations.javax_persistence_JoinTable??> + <#--Field: collection of entity--> <#assign entity = field.canonicalType?substring(field.canonicalType?index_of("<") + 1,field.canonicalType?length - 1)> + <#assign entityTable = JavaUtil.getEntityTableName(entity)> + <#assign table1 = tableName> + <#assign table2 = entityTable> <#if field.annotations.javax_persistence_JoinTable.name?has_content> <#assign refTableName = field.annotations.javax_persistence_JoinTable.name> <#else> - <#assign refTableName = tableName + "_" + JavaUtil.getEntityTableName(entity)> + <#assign refTableName = table1 + "_" + table2> - <#assign table1 = tableName> - <#if field.annotations.javax_persistence_JoinTable.joinColumns?has_content> - <#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> + <#--not yet support multiple JoinColumns or no JoinColumn--> + <#if field.annotations.javax_persistence_JoinTable.joinColumns?has_content + && field.annotations.javax_persistence_JoinTable.joinColumns?is_enumerable + && field.annotations.javax_persistence_JoinTable.joinColumns[0]?has_content> + <#assign col = field.annotations.javax_persistence_JoinTable.joinColumns[0].javax_persistence_JoinColumn> + <#assign name1 = col.name> + <#if col.referencedColumnName?has_content> + <#assign id1 = col.referencedColumnName> <#assign type1 = get_mapping_type(JavaUtil.getCanonicalNameOfField(pojo.canonicalName, id1))> + <#else> + <#assign result = JavaUtil.getPrimaryKey(pojo.canonicalName)?split(",")> + <#assign type1 = get_mapping_type(result[0])> + <#assign id1 = result[1]> <#else> - tbd + <#continue> - <#if field.annotations.javax_persistence_JoinTable.inverseJoinColumns?has_content> - <#assign refId2 = field.annotations.javax_persistence_JoinTable.inverseJoinColumns[0].javax_persistence_JoinColumn.name> + <#if field.annotations.javax_persistence_JoinTable.inverseJoinColumns?has_content + && field.annotations.javax_persistence_JoinTable.inverseJoinColumns?is_enumerable + && field.annotations.javax_persistence_JoinTable.inverseJoinColumns[0]?has_content> + <#assign col = field.annotations.javax_persistence_JoinTable.inverseJoinColumns[0].javax_persistence_JoinColumn> + <#assign name2 = col.name> + <#if col.referencedColumnName?has_content> + <#assign id2 = col.referencedColumnName> + <#assign type2 = get_mapping_type(JavaUtil.getCanonicalNameOfField(entity, id2))> + <#else> + <#assign result = JavaUtil.getPrimaryKey(entity)?split(",")> + <#assign type2 = get_mapping_type(result[0])> + <#assign id2 = result[1]> + + <#else> + <#continue> - <#assign refTables = refTables + [{"table": refTableName, "columns":[{"name": name1, "id": id1, "type": type1, "table": table1}, {"name": refId2, "id": "ID2", "type": "refType2", "table": "TB2"}] }]> + <#assign refTables = refTables + [{"table": refTableName, "columns":[{"name": name1, "id": id1, "type": type1, "table": table1}, {"name": name2, "id": id2, "type": type2, "table": table2}] }]> @@ -90,8 +117,8 @@ CREATE TABLE ${tableName} ( CREATE TABLE ${tb.table} ( <#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, + ${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}),