Merge pull request #6955 from timur-sh/6772

[#6772] Added a new flag `jpaVersion` to code generator.
This commit is contained in:
Lukas Eder 2017-12-29 12:11:07 +01:00 committed by GitHub
commit cc04a7fd81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 221 additions and 30 deletions

View File

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

View File

@ -77,6 +77,7 @@ 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;
@ -555,8 +556,18 @@ 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().isValidationAnnotations() != null)
generator.setGenerateValidationAnnotations(g.getGenerate().isValidationAnnotations());
if (g.getGenerate().isSpringAnnotations() != null)

View File

@ -38,6 +38,8 @@
package org.jooq.util;
import org.jooq.util.jaxb.JpaVersion;
import javax.annotation.Generated;
/**
@ -259,6 +261,16 @@ public interface Generator {
*/
void setGenerateJPAAnnotations(boolean generateJPAAnnotations);
/**
* Whether used a provided JPA spec version or latest version of it.
*/
JpaVersion generateJpaVersion();
/**
* Whether used a provided JPA spec version or latest version of it.
*/
void setGenerateJpaVersion(JpaVersion generateJpaVersion);
/**
* Whether POJO's and records should be annotated with JSR-303 validation
* annotations

View File

@ -129,6 +129,7 @@ import org.jooq.util.GeneratorStrategy.Mode;
// ...
// ...
// ...
import org.jooq.util.jaxb.JpaVersion;
import org.jooq.util.postgres.PostgresDatabase;
@ -278,6 +279,9 @@ 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(" validation annotations", generateValidationAnnotations());
log.info(" instance fields", generateInstanceFields());
log.info(" sequences", generateSequences());
@ -4439,43 +4443,45 @@ public class JavaGenerator extends AbstractGenerator {
out.print(scala ? ")" : "}");
}
StringBuilder sb2 = new StringBuilder();
String glue2 = "\n";
if (generateJpaVersion() != null && generateJpaVersion().supportedIndex()) {
StringBuilder sb2 = new StringBuilder();
String glue2 = "\n";
for (IndexDefinition index : table.getIndexes()) {
sb2.append(glue2);
sb2.append("\t")
.append(scala ? "new " : "@")
.append(out.ref("javax.persistence.Index"))
.append("(name = \"").append(index.getOutputName().replace("\"", "\\\"")).append("\"");
for (IndexDefinition index : table.getIndexes()) {
sb2.append(glue2);
sb2.append("\t")
.append(scala ? "new " : "@")
.append(out.ref("javax.persistence.Index"))
.append("(name = \"").append(index.getOutputName().replace("\"", "\\\"")).append("\"");
if (index.isUnique())
sb2.append(", unique = true");
if (index.isUnique())
sb2.append(", unique = true");
sb2.append(", columnList = \"");
sb2.append(", columnList = \"");
String glue2Inner = "";
for (IndexColumnDefinition column : index.getIndexColumns()) {
sb2.append(glue2Inner)
.append(column.getOutputName().replace("\"", "\\\""));
String glue2Inner = "";
for (IndexColumnDefinition column : index.getIndexColumns()) {
sb2.append(glue2Inner)
.append(column.getOutputName().replace("\"", "\\\""));
if (column.getSortOrder() == SortOrder.ASC)
sb2.append(" ASC");
else if (column.getSortOrder() == SortOrder.DESC)
sb2.append(" DESC");
if (column.getSortOrder() == SortOrder.ASC)
sb2.append(" ASC");
else if (column.getSortOrder() == SortOrder.DESC)
sb2.append(" DESC");
glue2Inner = ", ";
glue2Inner = ", ";
}
sb2.append("\")");
glue2 = ",\n";
}
sb2.append("\")");
glue2 = ",\n";
}
if (sb2.length() > 0) {
out.print(", indexes = ")
.print(scala ? "Array(" : "{");
out.println(sb2.toString());
out.print(scala ? ")" : "}");
if (sb2.length() > 0) {
out.print(", indexes = ")
.print(scala ? "Array(" : "{");
out.println(sb2.toString());
out.print(scala ? ")" : "}");
}
}
out.println(")");

View File

@ -16931,6 +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>
</ul>
<h3>Flags controlling record generation</h3>
@ -16994,6 +16995,7 @@ public class Book implements java.io.Serializable
<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>
</ul>
<h3>Flags controlling POJO generation</h3>

View File

@ -12,6 +12,7 @@ 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;
@ -80,6 +81,8 @@ public class Generate implements Serializable
protected Boolean daos = false;
@XmlElement(defaultValue = "false")
protected Boolean jpaAnnotations = false;
@XmlSchemaType(name = "string")
protected JpaVersion jpaVersion;
@XmlElement(defaultValue = "false")
protected Boolean validationAnnotations = false;
@XmlElement(defaultValue = "false")
@ -678,6 +681,31 @@ public class Generate implements Serializable
this.jpaAnnotations = value;
}
/**
* Sets the version of JPA specification to generate version-specific annotations.
* If it is omitted, the latest version is used by default.
*
* @return
* possible object is
* {@link JpaVersion }
*
*/
public JpaVersion getJpaVersion() {
return jpaVersion;
}
/**
* Sets the value of the jpaVersion property.
*
* @param value
* allowed object is
* {@link JpaVersion }
*
*/
public void setJpaVersion(JpaVersion value) {
this.jpaVersion = value;
}
/**
* Annotate POJOs and Records with JSR-303 validation annotations
*
@ -1299,6 +1327,11 @@ public class Generate implements Serializable
return this;
}
public Generate withJpaVersion(JpaVersion value) {
setJpaVersion(value);
return this;
}
public Generate withValidationAnnotations(Boolean value) {
setValidationAnnotations(value);
return this;

View File

@ -0,0 +1,102 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.util.jaxb;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* <p> Java class for JpaVersion
*
* <p>The following schema fragment specifies the expected content contained within this class.
* <p>
* <pre>
* &lt;simpleType name="JpaVersion"&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string"&gt;
* &lt;enumeration value="V_2_0"/&gt;
* &lt;enumeration value="V_2_1"/&gt;
* &lt;enumeration value="V_2_2"/&gt;
* &lt;/restriction&gt;
* &lt;/simpleType&gt;
* </pre>
*
* @author Timur Shaidullin
*/
@XmlType(name = "JpaVersion")
@XmlEnum
public enum JpaVersion {
V_2_0(null),
V_2_1(V_2_0, "Index"),
V_2_2(V_2_1);
private final List<String> annotations;
JpaVersion(JpaVersion previous) {
this(previous, new ArrayList<String>());
}
JpaVersion(JpaVersion previous, String... annotations) {
this(previous, new ArrayList<String>(Arrays.asList(annotations)));
}
JpaVersion(JpaVersion previous, List<String> annotations) {
this.annotations = annotations;
if (previous != null)
annotations.addAll(previous.annotations);
}
public List<String> getAnnotations() {
return annotations;
}
public boolean supportedIndex() {
return annotations.contains("Index");
}
public String getName() {
return name().substring(2).replace('_', '.');
}
public static JpaVersion latest() {
return V_2_2;
}
}

View File

@ -912,6 +912,10 @@ jOOQ version used for source code]]></jxb:javadoc></jxb:property></appinfo></ann
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Annotate POJOs and Records with JPA annotations.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="jpaVersion" type="tns:JpaVersion" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Version of JPA specification is to be used to generate version-specific annotations. If it is omitted, the latest version is used by default.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="validationAnnotations" type="boolean" default="false" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Annotate POJOs and Records with JSR-303 validation annotations]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
@ -1041,6 +1045,15 @@ 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>