可用版本:Dev (3.21) | 最新 (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11

JPADatabase:从实体生成代码

适用于 ✅ 开源版   ✅ 专业版   ✅ 企业版

许多 jOOQ 用户在主要使用 JPA 进行数据库交互的应用程序中使用 jOOQ 作为补充 SQL API,例如执行报表、批量处理、分析等。

在这种设置中,您可能有一个使用 JPA 注释实体实现的预先存在的模式。在开发时可能无法访问您的真实数据库模式,或者它不是您应用程序中的首要公民(即,您遵循 Java 优先的方法)。本节介绍如何从此 JPA 模型生成 jOOQ 类。考虑以下模型

@Entity
@Table(name = "author")
public class Author {

    @Id
    int       id;

    @Column(name = "first_name")
    String    firstName;

    @Column(name = "last_name")
    String    lastName;

    @OneToMany(mappedBy = "author")
    Set<Book> books;

    // Getters and setters...
}

@Entity
@Table(name = "book")
public class Book {

    @Id
    public int    id;

    @Column(name = "title")
    public String title;

    @ManyToOne
    public Author author;

    // Getters and setters...
}

现在,您可以不用将 jOOQ 代码生成器连接到保存上述模式表示的数据库,而是使用 jOOQ 的 JPADatabase 并将其提供给代码生成器。 JPADatabase 在内部使用 Hibernate,从您的实体生成内存中的 H2 数据库,并将该数据库反向工程回 jOOQ 类。

最简单的方法是使用 Maven 来包含 jooq-meta-extensions-hibernate 库(然后该库包含 H2 和 Hibernate 依赖项)

Maven
Gradle (Kotlin)
Gradle (Groovy)
<dependency>
                <!-- Use org.jooq                for the Open Source Edition
                         org.jooq.pro            for commercial editions with Java 21 support,
                         org.jooq.pro-java-17    for commercial editions with Java 17 support,
                         org.jooq.pro-java-11    for commercial editions with Java 11 support,
                         org.jooq.pro-java-8     for commercial editions with Java 8 support,
                         org.jooq.trial          for the free trial edition with Java 21 support,
                         org.jooq.trial-java-17  for the free trial edition with Java 17 support,
                         org.jooq.trial-java-11  for the free trial edition with Java 11 support,
                         org.jooq.trial-java-8   for the free trial edition with Java 8 support
    
                 Note: Only the Open Source Edition is hosted on Maven Central.
                       Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
                       See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk -->

    <groupId>org.jooq</groupId>
    <artifactId>jooq-meta-extensions-hibernate</artifactId>
    <version>3.20.5</version>
</dependency>
dependencies {
             // Use org.jooq                for the Open Source Edition
             //     org.jooq.pro            for commercial editions with Java 21 support,
             //     org.jooq.pro-java-17    for commercial editions with Java 17 support,
             //     org.jooq.pro-java-11    for commercial editions with Java 11 support,
             //     org.jooq.pro-java-8     for commercial editions with Java 8 support,
             //     org.jooq.trial          for the free trial edition with Java 21 support,
             //     org.jooq.trial-java-17  for the free trial edition with Java 17 support,
             //     org.jooq.trial-java-11  for the free trial edition with Java 11 support,
             //     org.jooq.trial-java-8   for the free trial edition with Java 8 support
             //
             // Note: Only the Open Source Edition is hosted on Maven Central.
             //       Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
             //       See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk

    jooqCodegen("org.jooq:jooq-meta-extensions-hibernate:3.20.5")
}
dependencies {
             // Use org.jooq                for the Open Source Edition
             //     org.jooq.pro            for commercial editions with Java 21 support,
             //     org.jooq.pro-java-17    for commercial editions with Java 17 support,
             //     org.jooq.pro-java-11    for commercial editions with Java 11 support,
             //     org.jooq.pro-java-8     for commercial editions with Java 8 support,
             //     org.jooq.trial          for the free trial edition with Java 21 support,
             //     org.jooq.trial-java-17  for the free trial edition with Java 17 support,
             //     org.jooq.trial-java-11  for the free trial edition with Java 11 support,
             //     org.jooq.trial-java-8   for the free trial edition with Java 8 support
             //
             // Note: Only the Open Source Edition is hosted on Maven Central.
             //       Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
             //       See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk

    jooqCodegen "org.jooq:jooq-meta-extensions-hibernate:3.20.5"
}

在添加该依赖项后,您现在可以在代码生成器配置中指定 JPADatabase

XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <database>
      <name>org.jooq.meta.extensions.jpa.JPADatabase</name>
      <properties>

        <!-- A comma separated list of Java packages, that contain your entities -->
        <property>
          <key>packages</key>
          <value>com.example.entities</value>
        </property>

        <!-- Whether JPA 2.1 AttributeConverters should be auto-mapped to jOOQ Converters.
             Custom <forcedType/> configurations will have a higher priority than these auto-mapped converters.
             This defaults to true. -->
        <property>
          <key>useAttributeConverters</key>
          <value>true</value>
        </property>
        <!-- The default schema for unqualified objects:

             - public: all unqualified objects are located in the PUBLIC (upper case) schema
             - none: all unqualified objects are located in the default schema (default)

             This configuration can be overridden with the schema mapping feature -->
        <property>
          <key>unqualifiedSchema</key>
          <value>none</value>
        </property>
      </properties>
    </database>
  </generator>
</configuration>

有关更多详细信息,请参见配置 XSD独立代码生成maven 代码生成

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(new Generator()
    .withDatabase(new Database()
      .withName("org.jooq.meta.extensions.jpa.JPADatabase")
      .withProperties(

        // A comma separated list of Java packages, that contain your entities
        new Property()
          .withKey("packages")
          .withValue("com.example.entities"),

        // Whether JPA 2.1 AttributeConverters should be auto-mapped to jOOQ Converters.
        // Custom <forcedType/> configurations will have a higher priority than these auto-mapped converters.
        // This defaults to true.
        new Property()
          .withKey("useAttributeConverters")
          .withValue(true),

        // The default schema for unqualified objects:
        // 
        // - public: all unqualified objects are located in the PUBLIC (upper case) schema
        // - none: all unqualified objects are located in the default schema (default)
        // 
        // This configuration can be overridden with the schema mapping feature
        new Property()
          .withKey("unqualifiedSchema")
          .withValue("none")
      )
    )
  )

有关更多详细信息,请参见配置 XSD程序化代码生成

import org.jooq.meta.jaxb.*


configuration {
  generator {
    database {
      name = "org.jooq.meta.extensions.jpa.JPADatabase"
      properties {

        // A comma separated list of Java packages, that contain your entities
        property {
          key = "packages"
          value = "com.example.entities"
        }

        // Whether JPA 2.1 AttributeConverters should be auto-mapped to jOOQ Converters.
        // Custom <forcedType/> configurations will have a higher priority than these auto-mapped converters.
        // This defaults to true.
        property {
          key = "useAttributeConverters"
          isValue = true
        }

        // The default schema for unqualified objects:
        // 
        // - public: all unqualified objects are located in the PUBLIC (upper case) schema
        // - none: all unqualified objects are located in the default schema (default)
        // 
        // This configuration can be overridden with the schema mapping feature
        property {
          key = "unqualifiedSchema"
          value = "none"
        }
      }
    }
  }
}

有关更多详细信息,请参见配置 XSDgradle 代码生成

configuration {
  generator {
    database {
      name = "org.jooq.meta.extensions.jpa.JPADatabase"
      properties {

        // A comma separated list of Java packages, that contain your entities
        property {
          key = "packages"
          value = "com.example.entities"
        }

        // Whether JPA 2.1 AttributeConverters should be auto-mapped to jOOQ Converters.
        // Custom <forcedType/> configurations will have a higher priority than these auto-mapped converters.
        // This defaults to true.
        property {
          key = "useAttributeConverters"
          value = true
        }

        // The default schema for unqualified objects:
        // 
        // - public: all unqualified objects are located in the PUBLIC (upper case) schema
        // - none: all unqualified objects are located in the default schema (default)
        // 
        // This configuration can be overridden with the schema mapping feature
        property {
          key = "unqualifiedSchema"
          value = "none"
        }
      }
    }
  }
}

有关更多详细信息,请参见配置 XSDgradle 代码生成

// The jOOQ-codegen-gradle plugin has been introduced in version 3.19.
// Please use the official plugin instead of the third party plugin that was recommended before.

以上将为您的 AUTHORBOOK 表生成所有 jOOQ 工件。

将设置传递给 Hibernate

当前版本的 jOOQ 在后台使用 Hibernate 生成内存中的 H2 数据库,从中反向工程 jOOQ 代码。为了影响 Hibernate 的模式生成,可以将 Hibernate 特定的标志传递给 MetadataSources。所有带有 hibernate.jakarta.persistence. 前缀的属性都将传递给 Hibernate。例如,这都是可能的

XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <database>
      <properties>
        <property>
          <key>hibernate.physical_naming_strategy</key>
          <value>org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy</value>
        </property>
      </properties>
    </database>
  </generator>
</configuration>

有关更多详细信息,请参见配置 XSD独立代码生成maven 代码生成

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(new Generator()
    .withDatabase(new Database()
      .withProperties(
        new Property()
          .withKey("hibernate.physical_naming_strategy")
          .withValue("org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy")
      )
    )
  )

有关更多详细信息,请参见配置 XSD程序化代码生成

import org.jooq.meta.jaxb.*


configuration {
  generator {
    database {
      properties {
        property {
          key = "hibernate.physical_naming_strategy"
          value = "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy"
        }
      }
    }
  }
}

有关更多详细信息,请参见配置 XSDgradle 代码生成

configuration {
  generator {
    database {
      properties {
        property {
          key = "hibernate.physical_naming_strategy"
          value = "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy"
        }
      }
    }
  }
}

有关更多详细信息,请参见配置 XSDgradle 代码生成

// The jOOQ-codegen-gradle plugin has been introduced in version 3.19.
// Please use the official plugin instead of the third party plugin that was recommended before.

如何组织您的依赖项

JPADatabase 将使用 Spring 从类路径中查找您的带注释的实体。这意味着您必须创建几个模块,其依赖关系图如下所示

                        +-------------------+
                        | Your JPA entities |
                        +-------------------+
                             ^         ^
                  depends on |         | depends on
                             |         |
          +---------------------+   +---------------------+
          | jOOQ codegen plugin |   | Your application    |
          +---------------------+   +---------------------+
                             |         |
                   generates |         | depends on
                             v         v
                     +-------------------------+
                     | jOOQ generated classes  |
                     +-------------------------+

您不能将 JPA 实体放在与运行 jOOQ 代码生成器的模块相同的模块中。另请参见代码生成依赖项

反馈

您对此页面有任何反馈吗? 我们很乐意听取您的意见!

The jOOQ Logo