Skip to content

Commit 3391914

Browse files
committed
Replace @IdClass with @embeddable for composite primary keys
1 parent b489c72 commit 3391914

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

src/main/java/org/ngbsn/generator/ModelGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.sf.jsqlparser.statement.create.table.Index;
88
import org.apache.commons.text.CaseUtils;
99
import org.ngbsn.model.Column;
10+
import org.ngbsn.model.EmbeddableClass;
1011
import org.ngbsn.model.ForeignKeyConstraint;
1112
import org.ngbsn.model.Table;
1213
import org.ngbsn.model.annotations.entityAnnotations.EntityAnnotation;
@@ -99,6 +100,12 @@ private static void extractPrimaryKeys(CreateTable parsedTable, Table table) {
99100
if (columnParamsList != null) {
100101
if (columnParamsList.size() > 1) {
101102
table.setNumOfPrimaryKeyColumns(columnParamsList.size());
103+
EmbeddableClass embeddableClass = EmbeddableClass
104+
.builder()
105+
.className(table.getClassName() + "PK")
106+
.fieldName(CaseUtils.toCamelCase(table.getName(), false, '_') + "PK")
107+
.build();
108+
table.setEmbeddableClass(embeddableClass);
102109
}
103110
List<Column> primaryKeyColumns = table.getColumns().stream().
104111
filter(column -> columnParamsList.stream().anyMatch(columnParams -> columnParams.getColumnName().equals(column.getName()))).toList();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.ngbsn.model;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Builder
8+
@Setter
9+
@Getter
10+
public class EmbeddableClass {
11+
private String className;
12+
private String fieldName;
13+
}

src/main/java/org/ngbsn/model/Table.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public class Table {
1717
private List<String> annotations = new ArrayList<>();
1818
private int numOfPrimaryKeyColumns;
1919
private List<ForeignKeyConstraint> foreignKeyConstraints = new ArrayList<>();
20+
private EmbeddableClass embeddableClass;
2021

2122
}

src/main/resources/templates/entity.ftl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,40 @@ import ${import};
77
<#list table.annotations as annotation>
88
${annotation}
99
</#list>
10-
<#if (table.numOfPrimaryKeyColumns > 1) >
11-
@IdClass(${table.className}.${table.className}PK.class)
12-
</#if>
10+
1311
public class ${table.className}{
1412
<#if (table.numOfPrimaryKeyColumns > 1) >
15-
static class ${table.className}PK implements Serializable{
13+
@Embeddable
14+
static class ${table.embeddableClass.className} implements Serializable{
1615
<#list table.columns as column>
1716
<#if column.primaryKey == true>
18-
private ${column.type} ${column.fieldName};
17+
<#list column.annotations as annotation>
18+
${annotation}
19+
</#list>
20+
private ${column.type} ${column.fieldName};
1921
</#if>
2022
</#list>
2123
}
22-
</#if>
2324

24-
<#list table.columns as column>
25-
<#if column.primaryKey == true>
25+
@EmbeddedId
26+
private ${table.embeddableClass.className} ${table.embeddableClass.fieldName};
27+
<#list table.columns as column>
28+
<#if column.primaryKey == false>
29+
<#list column.annotations as annotation>
30+
${annotation}
31+
</#list>
32+
private ${column.type} ${column.fieldName};
33+
</#if>
34+
</#list>
35+
<#else>
36+
<#list table.columns as column>
37+
<#if column.primaryKey == true>
2638
@Id
27-
</#if>
28-
<#list column.annotations as annotation>
39+
</#if>
40+
<#list column.annotations as annotation>
2941
${annotation}
30-
</#list>
42+
</#list>
3143
private ${column.type} ${column.fieldName};
32-
</#list>
44+
</#list>
45+
</#if>
3346
}

src/test/resources/sql/organization.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CREATE TABLE organization.titles (
5151
from_date DATE NOT NULL,
5252
to_date DATE,
5353
FOREIGN KEY (emp_no) REFERENCES organization.employees (emp_no) ON DELETE CASCADE,
54-
PRIMARY KEY (emp_no,title, from_date)
54+
PRIMARY KEY (title, from_date)
5555
);
5656

5757

0 commit comments

Comments
 (0)