[#6772] [#6955] Add a <jpaVersion/> code generator configuration

This commit is contained in:
lukaseder 2017-12-29 12:57:53 +01:00
parent cc04a7fd81
commit bbd5e69396
7 changed files with 40 additions and 48 deletions

View File

@ -45,7 +45,6 @@ import java.util.Collections;
import java.util.Set;
import org.jooq.tools.JooqLogger;
import org.jooq.util.jaxb.JpaVersion;
/**
@ -79,6 +78,7 @@ abstract class AbstractGenerator implements Generator {
boolean generateImmutableInterfaces = false;
boolean generateDaos = false;
boolean generateJPAAnnotations = false;
String generateJpaVersion = "";
boolean generateValidationAnnotations = false;
boolean generateSpringAnnotations = false;
boolean generateQueues = true;
@ -103,7 +103,6 @@ abstract class AbstractGenerator implements Generator {
boolean generateEmptyCatalogs = false;
boolean generateEmptySchemas = false;
boolean generatePrimaryKeyTypes = false;
JpaVersion generateJpaVersion;
protected GeneratorStrategyWrapper strategy;
protected String targetEncoding = "UTF-8";
@ -398,12 +397,12 @@ abstract class AbstractGenerator implements Generator {
}
@Override
public JpaVersion generateJpaVersion() {
public String generateJpaVersion() {
return generateJpaVersion;
}
@Override
public void setGenerateJpaVersion(JpaVersion generateJpaVersion) {
public void setGenerateJpaVersion(String generateJpaVersion) {
this.generateJpaVersion = generateJpaVersion;
}

View File

@ -77,7 +77,6 @@ import org.jooq.util.jaxb.Catalog;
import org.jooq.util.jaxb.Configuration;
import org.jooq.util.jaxb.Generate;
import org.jooq.util.jaxb.Jdbc;
import org.jooq.util.jaxb.JpaVersion;
import org.jooq.util.jaxb.Matchers;
import org.jooq.util.jaxb.Property;
import org.jooq.util.jaxb.Schema;
@ -556,18 +555,10 @@ public class GenerationTool {
generator.setGenerateImmutableInterfaces(g.getGenerate().isImmutableInterfaces());
if (g.getGenerate().isDaos() != null)
generator.setGenerateDaos(g.getGenerate().isDaos());
if (g.getGenerate().isJpaAnnotations() != null) {
if (g.getGenerate().isJpaAnnotations() != null)
generator.setGenerateJPAAnnotations(g.getGenerate().isJpaAnnotations());
if (g.getGenerate().getJpaVersion() != null)
generator.setGenerateJpaVersion(g.getGenerate().getJpaVersion());
else
generator.setGenerateJpaVersion(JpaVersion.latest());
}
else
generator.setGenerateJpaVersion(null);
if (g.getGenerate().getJpaVersion() != null)
generator.setGenerateJPAVersion(g.getGenerate().getJpaVersion());
if (g.getGenerate().isValidationAnnotations() != null)
generator.setGenerateValidationAnnotations(g.getGenerate().isValidationAnnotations());
if (g.getGenerate().isSpringAnnotations() != null)

View File

@ -38,8 +38,6 @@
package org.jooq.util;
import org.jooq.util.jaxb.JpaVersion;
import javax.annotation.Generated;
/**
@ -262,14 +260,14 @@ public interface Generator {
void setGenerateJPAAnnotations(boolean generateJPAAnnotations);
/**
* Whether used a provided JPA spec version or latest version of it.
* The minimum JPA version that is supported by generated code (latest version if empty).
*/
JpaVersion generateJpaVersion();
String generateJpaVersion();
/**
* Whether used a provided JPA spec version or latest version of it.
* The minimum JPA version that is supported by generated code (latest version if empty).
*/
void setGenerateJpaVersion(JpaVersion generateJpaVersion);
void setGenerateJpaVersion(String generateJpaVersion);
/**
* Whether POJO's and records should be annotated with JSR-303 validation

View File

@ -129,7 +129,6 @@ import org.jooq.util.GeneratorStrategy.Mode;
// ...
// ...
// ...
import org.jooq.util.jaxb.JpaVersion;
import org.jooq.util.postgres.PostgresDatabase;
@ -279,9 +278,7 @@ public class JavaGenerator extends AbstractGenerator {
+ ((!generateGeneratedAnnotation && (useSchemaVersionProvider || useCatalogVersionProvider)) ?
" (forced to true because of <schemaVersionProvider/> or <catalogVersionProvider/>)" : ""));
log.info(" JPA annotations", generateJPAAnnotations());
log.info(" JPA version", generateJpaVersion()
+ (!generateJPAAnnotations && generateJpaVersion != null ? " (forced to null because of <jpaAnnotations/>)" :
(generateJPAAnnotations && generateJpaVersion == null ? " (forced to " + JpaVersion.latest().name() + " because of <jpaVersion/>)" : "")));
log.info(" JPA version", generateJpaVersion());
log.info(" validation annotations", generateValidationAnnotations());
log.info(" instance fields", generateInstanceFields());
log.info(" sequences", generateSequences());
@ -4397,7 +4394,10 @@ public class JavaGenerator extends AbstractGenerator {
SchemaDefinition schema = table.getSchema();
if (generateJPAAnnotations()) {
// Since JPA 1.0
out.println("@%s", out.ref("javax.persistence.Entity"));
// Since JPA 1.0
out.print("@%s(name = \"", out.ref("javax.persistence.Table"));
out.print(table.getName().replace("\"", "\\\""));
out.print("\"");
@ -4418,6 +4418,8 @@ public class JavaGenerator extends AbstractGenerator {
sb1.append(glue1);
sb1.append("\t")
.append(scala ? "new " : "@")
// Since JPA 1.0
.append(out.ref("javax.persistence.UniqueConstraint")).append("(columnNames = ").append(scala ? "Array(" : "{");
String glue1Inner = "";
@ -4443,7 +4445,7 @@ public class JavaGenerator extends AbstractGenerator {
out.print(scala ? ")" : "}");
}
if (generateJpaVersion() != null && generateJpaVersion().supportedIndex()) {
if (StringUtils.isBlank(generateJpaVersion()) || "2.1".compareTo(generateJpaVersion()) <= 0) {
StringBuilder sb2 = new StringBuilder();
String glue2 = "\n";
@ -4495,8 +4497,11 @@ public class JavaGenerator extends AbstractGenerator {
if (pk != null) {
if (pk.getKeyColumns().size() == 1) {
// Since JPA 1.0
out.tab(1).println("@%s", out.ref("javax.persistence.Id"));
// Since JPA 1.0
if (pk.getKeyColumns().get(0).isIdentity())
out.tab(1).println("@%s(strategy = %s.IDENTITY)",
out.ref("javax.persistence.GeneratedValue"),
@ -4532,6 +4537,7 @@ public class JavaGenerator extends AbstractGenerator {
}
}
// Since JPA 1.0
out.print("\t@%s(name = \"", out.ref("javax.persistence.Column"));
out.print(column.getName().replace("\"", "\\\""));
out.print("\"");

View File

@ -16931,7 +16931,7 @@ implements IBook {
<li><strong>daos</strong>: Records are a pre-requisite for DAOs. If DAOs are generated, records are generated as well</li>
<li><strong>interfaces</strong>: If interfaces are generated, records will implement them</li>
<li><strong>jpaAnnotations</strong>: JPA annotations are used on generated records</li>
<li><strong>jpaVersion</strong>: Version of JPA specification is to be used to generate version-specific annotations. If it is omitted, the latest version is used by default.</li>
<li><strong>jpaVersion</strong>: Version of JPA specification is to be used to generate version-specific annotations. If it is omitted, the latest version is used by default.</li>
</ul>
<h3>Flags controlling record generation</h3>
@ -16994,8 +16994,8 @@ public class Book implements java.io.Serializable
<li><strong>immutablePojos</strong>: Immutable POJOs have final members and no setters. All members must be passed to the constructor</li>
<li><strong>daos</strong>: POJOs are a pre-requisite for DAOs. If DAOs are generated, POJOs are generated as well</li>
<li><strong>jpaAnnotations</strong>: JPA annotations are used on generated records</li>
<li><strong>validationAnnotations</strong>: JSR-303 validation annotations are used on generated records</li>
<li><strong>jpaVersion</strong>: Version of JPA specification is to be used to generate version-specific annotations. If it is omitted, the latest version is used by default.</li>
<li><strong>validationAnnotations</strong>: JSR-303 validation annotations are used on generated records</li>
</ul>
<h3>Flags controlling POJO generation</h3>

View File

@ -12,7 +12,6 @@ import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.jooq.util.jaxb.tools.StringAdapter;
@ -81,8 +80,8 @@ public class Generate implements Serializable
protected Boolean daos = false;
@XmlElement(defaultValue = "false")
protected Boolean jpaAnnotations = false;
@XmlSchemaType(name = "string")
protected JpaVersion jpaVersion;
@XmlJavaTypeAdapter(StringAdapter.class)
protected String jpaVersion;
@XmlElement(defaultValue = "false")
protected Boolean validationAnnotations = false;
@XmlElement(defaultValue = "false")
@ -682,15 +681,14 @@ public class Generate implements Serializable
}
/**
* Sets the version of JPA specification to generate version-specific annotations.
* If it is omitted, the latest version is used by default.
* Version of JPA specification is to be used to generate version-specific annotations. If it is omitted, the latest version is used by default.
*
* @return
* possible object is
* {@link JpaVersion }
* {@link String }
*
*/
public JpaVersion getJpaVersion() {
public String getJpaVersion() {
return jpaVersion;
}
@ -699,10 +697,10 @@ public class Generate implements Serializable
*
* @param value
* allowed object is
* {@link JpaVersion }
* {@link String }
*
*/
public void setJpaVersion(JpaVersion value) {
public void setJpaVersion(String value) {
this.jpaVersion = value;
}
@ -1327,7 +1325,7 @@ public class Generate implements Serializable
return this;
}
public Generate withJpaVersion(JpaVersion value) {
public Generate withJpaVersion(String value) {
setJpaVersion(value);
return this;
}

View File

@ -1045,15 +1045,6 @@ This flag is ignored in the commercial Java 6 distribution of jOOQ 3.9+ ]]></jxb
</all>
</complexType>
<simpleType name="JpaVersion">
<restriction base="string">
<enumeration value="V_2_0"/>
<enumeration value="V_2_1"/>
<enumeration value="V_2_2"/>
</restriction>
</simpleType>
<complexType name="Target">
<annotation><appinfo><jxb:class><jxb:javadoc><![CDATA[Options to define where the generated code should be located.]]></jxb:javadoc></jxb:class></appinfo></annotation>
<all>
@ -1091,4 +1082,13 @@ e.g. org.jooq.generated.schema1, org.jooq.generated.schema2]]></jxb:javadoc></jx
<enumeration value="UNICODE_CHARACTER_CLASS"/>
</restriction>
</simpleType>
<simpleType name="JpaVersion">
<restriction base="string">
<enumeration value="1.0"/>
<enumeration value="2.0"/>
<enumeration value="2.1"/>
<enumeration value="2.2"/>
</restriction>
</simpleType>
</schema>