From 97cab67e4a030780efe8bafa0ab823621a548a5b Mon Sep 17 00:00:00 2001 From: lukaseder Date: Wed, 17 Apr 2019 11:26:59 +0200 Subject: [PATCH] [#8535] Generate @UniqueConstraint annotation instead of @Column(unique = true) The current distinction is confusing, and if redundant uniqueness information is present, then Hibernate ignores the @UniqueConstraint name. Hence, we will only generate the @UniqueConstraint annotation --- .../java/org/jooq/codegen/JavaGenerator.java | 49 +++++++------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java index 37b57d3c2f..c973647d92 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java @@ -4886,30 +4886,26 @@ public class JavaGenerator extends AbstractGenerator { String glue1 = generateNewline(); for (UniqueKeyDefinition uk : table.getUniqueKeys()) { + sb1.append(glue1); + sb1.append("\t") + .append(scala ? "new " : "@") - // Single-column keys are annotated on the column itself - if (uk.getKeyColumns().size() > 1) { - sb1.append(glue1); - sb1.append("\t") - .append(scala ? "new " : "@") + // Since JPA 1.0 + .append(out.ref("javax.persistence.UniqueConstraint")).append("(columnNames = ").append(scala ? "Array(" : "{"); - // Since JPA 1.0 - .append(out.ref("javax.persistence.UniqueConstraint")).append("(columnNames = ").append(scala ? "Array(" : "{"); + String glue1Inner = ""; + for (ColumnDefinition column : uk.getKeyColumns()) { + sb1.append(glue1Inner); + sb1.append("\""); + sb1.append(column.getName().replace("\"", "\\\"")); + sb1.append("\""); - String glue1Inner = ""; - for (ColumnDefinition column : uk.getKeyColumns()) { - sb1.append(glue1Inner); - sb1.append("\""); - sb1.append(column.getName().replace("\"", "\\\"")); - sb1.append("\""); - - glue1Inner = ", "; - } - - sb1.append(scala ? ")" : "}").append(")"); - - glue1 = "," + generateNewline(); + glue1Inner = ", "; } + + sb1.append(scala ? ")" : "}").append(")"); + + glue1 = "," + generateNewline(); } if (sb1.length() > 0) { @@ -4967,7 +4963,6 @@ public class JavaGenerator extends AbstractGenerator { protected void printColumnJPAAnnotation(JavaWriter out, ColumnDefinition column) { if (generateJPAAnnotations()) { UniqueKeyDefinition pk = column.getPrimaryKey(); - List uks = column.getUniqueKeys(); if (pk != null) { if (pk.getKeyColumns().size() == 1) { @@ -4983,14 +4978,6 @@ public class JavaGenerator extends AbstractGenerator { } } - String unique = ""; - for (UniqueKeyDefinition uk : uks) { - if (uk.getKeyColumns().size() == 1) { - unique = ", unique = true"; - break; - } - } - String nullable = ""; if (!column.getType(resolver()).isNullable()) { nullable = ", nullable = false"; @@ -5011,11 +4998,13 @@ public class JavaGenerator extends AbstractGenerator { } } + // [#8535] The unique flag is not set on the column, but only on + // the table's @UniqueConstraint section. + // Since JPA 1.0 out.print("\t@%s(name = \"", out.ref("javax.persistence.Column")); out.print(column.getName().replace("\"", "\\\"")); out.print("\""); - out.print(unique); out.print(nullable); out.print(length); out.print(precision);