我有以下配置,对我来说效果很好:
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)))
;我知道这缺少某些字段/getter的定义,但请忽略这一点和我的额外注释(它们与问题无关)。
我知道我们可以使用withExcludes选项来提供一个正则表达式,它指示我们希望从数据库生成中排除哪些数据库对象。在上面的配置中,我有以下配置:
.withExcludes(getExcludeList())这可以很好地从自动生成的类中完全排除数据库对象。然而,我的问题是:是否有一个类似于上面的选项可以简单地将生成的类排除在JPA注释之外?我仍然希望这些数据库对象生成类,但我不希望它们有JPA注释。目前,我使用以下选项:
.withJpaAnnotations(true)
.withJpaVersion("2.2")这些选项在基本上所有内容(视图、表值函数等)上生成JPA注释。我希望避免为某些不必要的数据库对象生成它。
可能是这样的:
.withJpaAnnotations(true)
.withJpaVersion("2.2")
.withJpaAnnotationsExcludes(...)发布于 2020-04-25 23:52:32
这方面没有现成的配置,但在这种情况下,您可以通过覆盖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
}
}https://stackoverflow.com/questions/61425341
复制相似问题