Available in versions: Dev (3.19) | Latest (3.18) | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11 | 3.10 | 3.9
Configuration and setup of the generator
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
There are three binaries available with jOOQ, to be downloaded from https://www.jooq.org/download or from Maven central:
-
jooq-3.9.6.jar
The main library that you will include in your application to run jOOQ -
jooq-meta-3.9.6.jar
The utility that you will include in your build to navigate your database schema for code generation. This can be used as a schema crawler as well. -
jooq-codegen-3.9.6.jar
The utility that you will include in your build to generate your database schema
Configure jOOQ's code generator
You need to tell jOOQ some things about your database connection. Here's an example of how to do it for an Oracle database
<configuration> <!-- Configure the database connection here --> <jdbc> <driver>oracle.jdbc.OracleDriver</driver> <url>jdbc:oracle:thin:@[your jdbc connection parameters]</url> <user>[your database user]</user> <password>[your database password]</password> <!-- You can also pass user/password and other JDBC properties in the optional properties tag: --> <properties> <property><key>user</key><value>[db-user]</value></property> <property><key>password</key><value>[db-password]</value></property> </properties> </jdbc> <generator> <database> <!-- The database dialect from jooq-meta. Available dialects are named org.jooq.util.[database].[database]Database. Natively supported values are: org.jooq.util.ase.ASEDatabase org.jooq.util.cubrid.CUBRIDDatabase org.jooq.util.db2.DB2Database org.jooq.util.derby.DerbyDatabase org.jooq.util.firebird.FirebirdDatabase org.jooq.util.h2.H2Database org.jooq.util.hana.HANADatabase org.jooq.util.hsqldb.HSQLDBDatabase org.jooq.util.informix.InformixDatabase org.jooq.util.ingres.IngresDatabase org.jooq.util.mariadb.MariaDBDatabase org.jooq.util.mysql.MySQLDatabase org.jooq.util.oracle.OracleDatabase org.jooq.util.postgres.PostgresDatabase org.jooq.util.redshift.RedshiftDatabase org.jooq.util.sqlite.SQLiteDatabase org.jooq.util.sqlserver.SQLServerDatabase org.jooq.util.sybase.SybaseDatabase org.jooq.util.vertica.VerticaDatabase This value can be used to reverse-engineer generic JDBC DatabaseMetaData (e.g. for MS Access) org.jooq.util.jdbc.JDBCDatabase This value can be used to reverse-engineer standard jOOQ-meta XML formats org.jooq.util.xml.XMLDatabase You can also provide your own org.jooq.util.Database implementation here, if your database is currently not supported Note the classes have been moved to org.jooq.meta in jOOQ 3.11 --> <name>org.jooq.util.oracle.OracleDatabase</name> <!-- All elements that are generated from your schema (A Java regular expression. Use the pipe to separate several expressions) Watch out for case-sensitivity. Depending on your database, this might be important! You can create case-insensitive regular expressions using this syntax: (?i:expr) Whitespace is ignored and comments are possible. --> <includes>.*</includes> <!-- All elements that are excluded from your schema (A Java regular expression. Use the pipe to separate several expressions). Excludes match before includes, i.e. excludes have a higher priority --> <excludes> UNUSED_TABLE # This table (unqualified name) should not be generated | PREFIX_.* # Objects with a given prefix should not be generated | SECRET_SCHEMA\.SECRET_TABLE # This table (qualified name) should not be generated | SECRET_ROUTINE # This routine (unqualified name) ... </excludes> <!-- The schema that is used locally as a source for meta information. This could be your development schema or the production schema, etc This cannot be combined with the schemata element. If left empty, jOOQ will generate all available schemata. See the manual's next section to learn how to generate several schemata --> <inputSchema>[your database schema / owner / name]</inputSchema> </database> <!-- Generation flags: See advanced configuration properties --> <generate/> <target> <!-- The destination package of your generated classes (within the destination directory) jOOQ may append the schema name to this package if generating multiple schemas, e.g. org.jooq.your.packagename.schema1 org.jooq.your.packagename.schema2 --> <packageName>org.jooq.your.packagename</packageName> <!-- The destination directory of your generated classes --> <directory>/path/to/your/dir</directory> </target> </generator> </configuration>
See the configuration XSD, standalone code generation, and maven code generation for more details.
new org.jooq.util.jaxb.Configuration() // Configure the database connection here .withJdbc(new Jdbc() .withDriver("oracle.jdbc.OracleDriver") .withUrl("jdbc:oracle:thin:@[your jdbc connection parameters]") .withUser("[your database user]") .withPassword("[your database password]") // You can also pass user/password and other JDBC properties in the optional properties tag: .withProperties( new Property() .withKey("user") .withValue("[db-user]"), new Property() .withKey("password") .withValue("[db-password]") ) ) .withGenerator(new Generator() .withDatabase(new Database() // The database dialect from jooq-meta. Available dialects are // named org.jooq.util.[database].[database]Database. // // Natively supported values are: // // org.jooq.util.ase.ASEDatabase // org.jooq.util.cubrid.CUBRIDDatabase // org.jooq.util.db2.DB2Database // org.jooq.util.derby.DerbyDatabase // org.jooq.util.firebird.FirebirdDatabase // org.jooq.util.h2.H2Database // org.jooq.util.hana.HANADatabase // org.jooq.util.hsqldb.HSQLDBDatabase // org.jooq.util.informix.InformixDatabase // org.jooq.util.ingres.IngresDatabase // org.jooq.util.mariadb.MariaDBDatabase // org.jooq.util.mysql.MySQLDatabase // org.jooq.util.oracle.OracleDatabase // org.jooq.util.postgres.PostgresDatabase // org.jooq.util.redshift.RedshiftDatabase // org.jooq.util.sqlite.SQLiteDatabase // org.jooq.util.sqlserver.SQLServerDatabase // org.jooq.util.sybase.SybaseDatabase // org.jooq.util.vertica.VerticaDatabase // // This value can be used to reverse-engineer generic JDBC DatabaseMetaData (e.g. for MS Access) // // org.jooq.util.jdbc.JDBCDatabase // // This value can be used to reverse-engineer standard jOOQ-meta XML formats // // org.jooq.util.xml.XMLDatabase // // You can also provide your own org.jooq.util.Database implementation // here, if your database is currently not supported // // Note the classes have been moved to org.jooq.meta in jOOQ 3.11 .withName("org.jooq.util.oracle.OracleDatabase") // All elements that are generated from your schema (A Java regular expression. // Use the pipe to separate several expressions) Watch out for // case-sensitivity. Depending on your database, this might be // important! // // You can create case-insensitive regular expressions using this syntax: (?i:expr) // // Whitespace is ignored and comments are possible. .withIncludes(".*") // All elements that are excluded from your schema (A Java regular expression. // Use the pipe to separate several expressions). Excludes match before // includes, i.e. excludes have a higher priority .withExcludes(""" UNUSED_TABLE # This table (unqualified name) should not be generated | PREFIX_.* # Objects with a given prefix should not be generated | SECRET_SCHEMA\.SECRET_TABLE # This table (qualified name) should not be generated | SECRET_ROUTINE # This routine (unqualified name) ... """) // The schema that is used locally as a source for meta information. // This could be your development schema or the production schema, etc // This cannot be combined with the schemata element. // // If left empty, jOOQ will generate all available schemata. See the // manual's next section to learn how to generate several schemata .withInputSchema("[your database schema / owner / name]") ) // Generation flags: See advanced configuration properties .withGenerate() .withTarget(new Target() // The destination package of your generated classes (within the // destination directory) // // jOOQ may append the schema name to this package if generating multiple schemas, // e.g. org.jooq.your.packagename.schema1 // org.jooq.your.packagename.schema2 .withPackageName("org.jooq.your.packagename") // The destination directory of your generated classes .withDirectory("/path/to/your/dir") ) )
See the configuration XSD and programmatic code generation for more details.
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19 only.
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19 only.
generationTool { // Configure the database connection here jdbc { driver = "oracle.jdbc.OracleDriver" url = "jdbc:oracle:thin:@[your jdbc connection parameters]" user = "[your database user]" password = "[your database password]" // You can also pass user/password and other JDBC properties in the optional properties tag: properties { property { key = "user" value = "[db-user]" } property { key = "password" value = "[db-password]" } } } generator { database { // The database dialect from jooq-meta. Available dialects are // named org.jooq.util.[database].[database]Database. // // Natively supported values are: // // org.jooq.util.ase.ASEDatabase // org.jooq.util.cubrid.CUBRIDDatabase // org.jooq.util.db2.DB2Database // org.jooq.util.derby.DerbyDatabase // org.jooq.util.firebird.FirebirdDatabase // org.jooq.util.h2.H2Database // org.jooq.util.hana.HANADatabase // org.jooq.util.hsqldb.HSQLDBDatabase // org.jooq.util.informix.InformixDatabase // org.jooq.util.ingres.IngresDatabase // org.jooq.util.mariadb.MariaDBDatabase // org.jooq.util.mysql.MySQLDatabase // org.jooq.util.oracle.OracleDatabase // org.jooq.util.postgres.PostgresDatabase // org.jooq.util.redshift.RedshiftDatabase // org.jooq.util.sqlite.SQLiteDatabase // org.jooq.util.sqlserver.SQLServerDatabase // org.jooq.util.sybase.SybaseDatabase // org.jooq.util.vertica.VerticaDatabase // // This value can be used to reverse-engineer generic JDBC DatabaseMetaData (e.g. for MS Access) // // org.jooq.util.jdbc.JDBCDatabase // // This value can be used to reverse-engineer standard jOOQ-meta XML formats // // org.jooq.util.xml.XMLDatabase // // You can also provide your own org.jooq.util.Database implementation // here, if your database is currently not supported // // Note the classes have been moved to org.jooq.meta in jOOQ 3.11 name = "org.jooq.util.oracle.OracleDatabase" // All elements that are generated from your schema (A Java regular expression. // Use the pipe to separate several expressions) Watch out for // case-sensitivity. Depending on your database, this might be // important! // // You can create case-insensitive regular expressions using this syntax: (?i:expr) // // Whitespace is ignored and comments are possible. includes = ".*" // All elements that are excluded from your schema (A Java regular expression. // Use the pipe to separate several expressions). Excludes match before // includes, i.e. excludes have a higher priority excludes = """ UNUSED_TABLE # This table (unqualified name) should not be generated | PREFIX_.* # Objects with a given prefix should not be generated | SECRET_SCHEMA\.SECRET_TABLE # This table (qualified name) should not be generated | SECRET_ROUTINE # This routine (unqualified name) ... """ // The schema that is used locally as a source for meta information. // This could be your development schema or the production schema, etc // This cannot be combined with the schemata element. // // If left empty, jOOQ will generate all available schemata. See the // manual's next section to learn how to generate several schemata inputSchema = "[your database schema / owner / name]" } // Generation flags: See advanced configuration properties generate {} target { // The destination package of your generated classes (within the // destination directory) // // jOOQ may append the schema name to this package if generating multiple schemas, // e.g. org.jooq.your.packagename.schema1 // org.jooq.your.packagename.schema2 packageName = "org.jooq.your.packagename" // The destination directory of your generated classes directory = "/path/to/your/dir" } } }
See the configuration XSD and gradle code generation for more details.
There are also lots of advanced configuration parameters, which will be treated in the manual's section about advanced code generation features Note, you can find the official XSD file for a formal specification at:
https://www.jooq.org/xsd/jooq-codegen-3.9.2.xsd
Run jOOQ code generation
Code generation works by calling this class with the above property file as argument.
org.jooq.util.GenerationTool /jooq-config.xml
Note: The GenerationTool
class has been moved to org.jooq.codegen
in jOOQ 3.11
Be sure that these elements are located on the classpath:
- The XML configuration file
- jooq-3.9.6.jar, jooq-meta-3.9.6.jar, jooq-codegen-3.9.6.jar
- The JDBC driver you configured
A command-line example (For Windows, unix/linux/etc will be similar)
- Put the property file, jooq*.jar and the JDBC driver into a directory, e.g. C:\temp\jooq
- Go to C:\temp\jooq
- Run java -cp jooq-3.9.6.jar;jooq-meta-3.9.6.jar;jooq-codegen-3.9.6.jar;[JDBC-driver].jar;. org.jooq.util.GenerationTool /[XML file]
Note that the property file must be passed as a classpath resource
Run code generation from Eclipse
Of course, you can also run code generation from your IDE. In Eclipse, set up a project like this. Note that:
- this example uses jOOQ's log4j support by adding log4j.xml and log4j.jar to the project classpath.
- the actual jooq-3.9.6.jar, jooq-meta-3.9.6.jar, jooq-codegen-3.9.6.jar artefacts may contain version numbers in the file names.
Once the project is set up correctly with all required artefacts on the classpath, you can configure an Eclipse Run Configuration for org.jooq.util.GenerationTool.
With the XML file as an argument
And the classpath set up correctly
Finally, run the code generation and see your generated artefacts
Integrate generation with Maven
Using the official jOOQ-codegen-maven plugin, you can integrate source code generation in your Maven build process:
<plugin> <!-- Specify the maven code generator plugin --> <!-- Use org.jooq for the Open Source Edition org.jooq.pro for commercial editions, org.jooq.pro-java-6 for commercial editions with Java 6 support, org.jooq.trial for the free trial edition 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 --> <groupId>org.jooq</groupId> <artifactId>jooq-codegen-maven</artifactId> <version>3.9.6</version> <!-- The plugin should hook into the generate goal --> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <!-- Manage the plugin's dependency. In this example, we'll use a PostgreSQL database --> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4.1212</version> </dependency> </dependencies> <!-- Specify the plugin configuration. The configuration format is the same as for the standalone code generator --> <configuration> <!-- JDBC connection parameters --> <jdbc> <driver>org.postgresql.Driver</driver> <url>jdbc:postgresql:postgres</url> <user>postgres</user> <password>test</password> </jdbc> <!-- Generator parameters --> <generator> <database> <includes>.*</includes> <excludes></excludes> <!-- In case your database supports catalogs, e.g. SQL Server: <inputCatalog>public</inputCatalog> --> <inputSchema>public</inputSchema> </database> <target> <packageName>org.jooq.util.maven.example</packageName> <directory>target/generated-sources/jooq</directory> </target> </generator> </configuration> </plugin>
Use jOOQ generated classes in your application
Be sure, both jooq-3.9.6.jar and your generated package (see configuration) are located on your classpath. Once this is done, you can execute SQL statements with your generated classes.
Feedback
Do you have any feedback about this page? We'd love to hear it!