如何排除特定的 DB 对象使其不具有为它们生成的 JPA 注释?

How can I exclude specific DB objects from having JPA annotations generated for them?

我有以下适合我的配置:

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)))
  ;

我知道这缺少某些 fields/getters 的定义,但请忽略它和我的额外评论(它们与问题无关)。

我知道我们可以使用 withExcludes 选项给出一个正则表达式,它指示我们要从数据库生成中排除哪些数据库对象。在上面的配置中,我有以下配置:

      .withExcludes(getExcludeList())

这可以很好地从自动生成的 classes 中完全排除数据库对象。但是,我的问题是:是否有一个我可以使用的选项类似于上面的选项,它指示简单地将生成的 class 排除在包含 JPA 注释之外?我仍然希望那些数据库对象生成 classes,但我不希望它们具有 JPA 注释。目前我使用选项:

  .withJpaAnnotations(true)
  .withJpaVersion("2.2")

这些选项基本上在所有内容(视图、table 值函数等)上生成 JPA 注释。我想避免为某些不必要的数据库对象生成它。

可能是这样的:

  .withJpaAnnotations(true)
  .withJpaVersion("2.2")
  .withJpaAnnotationsExcludes(...)

没有开箱即用的配置,但在这种特殊情况下,您可以通过覆盖 org.jooq.codegen.JavaGenerator class 及其两个方法(您似乎已经在这样做了):

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
    }
}