vab2048
vab2048

Reputation: 1255

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

I have the following configuration which works well for me:

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

I am aware this is missing the definitions of certain fields/getters but please ignore that and my extra comments (they are not relevant to the question).

I know we can use the withExcludes option to give a regular expression which indicates what database objects we want to exclude from the database generation. In the config above I have the following config:

      .withExcludes(getExcludeList())

This works well to exclude database objects completely from the auto-generated classes. However, my question is: is there an option I can use similar to the one above which indicates to simply exclude a generated class from including JPA annotations? I still want those database objects to have classes generated, but I don't want them to have JPA annotations. Currently I use the options:

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

These options generate JPA annotations on basically everything (views, table-valued functions, etc). And I would like to avoid it being generated for certain, unnecessary database objects.

Maybe something like:

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

Upvotes: 1

Views: 397

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221106

There is no out of the box configuration for this, but in this particular case, you can relatively easily achieve the wanted behaviour by overriding the org.jooq.codegen.JavaGenerator class and two of its methods (you seem to be doing this already):

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

Upvotes: 1

Related Questions