如何从为它们生成的JPA注释中排除特定的数据库对象?

时间:2020-04-25 12:06:01

标签: jooq

我有以下配置对我来说很好:

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

我知道这缺少某些字段/获取器的定义,但请忽略此内容以及我的多余评论(它们与问题无关)。

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

      .withExcludes(getExcludeList())

这很好地将数据库对象完全排除在自动生成的类之外。但是,我的问题是:是否可以使用与上述选项类似的选项,该选项表示仅将生成的类排除在包含JPA注释的范围之外?我仍然希望这些数据库对象具有生成的类,但是我不希望它们具有JPA批注。目前,我使用以下选项:

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

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

也许是这样的:

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

1 个答案:

答案 0 :(得分:1)

没有开箱即用的配置,但是在这种特殊情况下,您可以通过覆盖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
    }
}
相关问题