我有下面的配置,它对我来说效果很好。
return new org.jooq.meta.jaxb.Configuration()
.withJdbc(new Jdbc()
.withDriver(dataSourceDriver)
.withUrl(dataSourceUrl)
.withUser(username)
.withPassword(password))
.withGenerator(new Generator()
.withName(CustomJooqGenerator.class.getCanonicalName())
// Generation options, see: https://www.jooq.org/doc/3.4/manual/code-generation/codegen-advanced/
.withGenerate(new Generate()
/* ******************************************
* Class/Record Generation Option
* ******************************************/
// Generate jOOQ Record classes for type-safe querying. You can turn
// this off, if you don't need "active records" for CRUD.
.withRecords(true)
// Generate POJOs in addition to Record classes for usage of the
// ResultQuery.fetchInto(Class) API.
.withPojos(true)
// Generate data access objects (DAOs) in addition to other classes.
.withDaos(true)
/* ******************************************
* Annotation Generation
* - see https://www.jooq.org/doc/3.12/manual/code-generation/codegen-advanced/codegen-config-generate/codegen-generate-annotations/
* ******************************************/
// Place the javax.annotation.Generated annotation on generated java files
// to indicate the jOOQ version used for source code. Defaults to true.
.withGeneratedAnnotation(true)
// Possible values for generatedAnnotationType:
// DETECT_FROM_JDK | JAVAX_ANNOTATION_GENERATED |
// JAVAX_ANNOTATION_PROCESSING_GENERATED
.withGeneratedAnnotationType(DETECT_FROM_JDK)
// Annotate POJOs and Records with JPA annotations for increased
// compatibility and better integration with JPA/Hibernate, etc
.withJpaAnnotations(true)
.withJpaVersion("2.2")
// Annotate POJOs and Records with JSR-303 validation annotations.
.withValidationAnnotations(true)
// Spring annotations can be applied on DAOs for better Spring integration. These include:
// @Autowired, and @Repository.
.withSpringAnnotations(true))
.withDatabase(new Database()
.withName("org.jooq.meta.postgres.PostgresDatabase")
.withIncludes(".*")
.withExcludes(getExcludeList())
// Remove withSchemata to generate for every schema and catalogue.
// Currently, this has issues with type generation for the default
// catalogue, so we pass in a list of schemas we are interested in.
.withSchemata(getSchemas())
// See: https://www.jooq.org/doc/3.13/manual/code-generation/custom-data-type-bindings/
// Forces certain DB types to be mapped to Java types.
.withForcedTypes(getForcedTypes())
)
.withTarget(new Target()
.withPackageName(generatedSourcesOutputPackageName)
.withDirectory(generationOutputDir)))
;
我知道这缺少了某些字段getters的定义,但请忽略这一点和我额外的注释(它们与问题无关)。
我知道我们可以使用 withExcludes
选项来给出一个正则表达式,用来指示我们要从数据库生成中排除哪些数据库对象。在上面的配置中,我有如下配置。
.withExcludes(getExcludeList())
这很好地将数据库对象从自动生成的类中完全排除。然而,我的问题是:有没有类似于上面的选项可以让我简单地排除生成的类中包含JPA注解?我仍然希望那些数据库对象有生成的类,但我不希望它们有JPA注释。目前我使用的是选项。
.withJpaAnnotations(true)
.withJpaVersion("2.2")
这些选项基本上会在所有的东西上生成JPA注解(视图,表值函数等)。而我希望避免对某些不必要的数据库对象生成。
也许是这样的。
.withJpaAnnotations(true)
.withJpaVersion("2.2")
.withJpaAnnotationsExcludes(...)
没有开箱即用的配置,但在这种特殊情况下,你可以比较容易地通过重写的 org.jooq.codegen.JavaGenerator
类和它的两个方法(你似乎已经这样做了)。
public class CustomJooqGenerator extends JavaGenerator {
@Override
protected void printTableJPAAnnotation(JavaWriter out, TableDefinition table) {
if (someCondition)
super.printTableJPAAnnotation(out, table);
else
; // Don't do anything
}
@Override
protected void printColumnJPAAnnotation(JavaWriter out, ColumnDefinition column) {
if (someCondition)
super.printColumnJPAAnnotation(out, column);
else
; // Don't do anything
}
}