Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12
Reducing the size of generated code
Supported by ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
jOOQ's generated code contains a lot of convenience, much of which is optional. For most schemas, this is not a problem, but for very large schemas with thousands of tables, foreign keys, or routines, the sheer amount of generated code can become a burden:
- Compilation (
javac,scalac) gets slow and may require a lot of memory, especially with complex generic types - IDEs need more time and memory to index the generated sources
- The resulting artefacts get larger, and class loading at runtime takes longer
While the section about code generation for large schemas deals with hard limits of the compiler and the JVM, this section summarises the code generation flags that can be used to reduce the overall size of generated code, and the functionality that you give up when doing so. Typically, you will want to turn off only those features that you don't need in your project.
Generate fewer objects
The most effective way of reducing the size of generated code is not to generate objects that you don't need at all:
- Use <includes/> and <excludes/> to restrict code generation to the tables, routines, etc. that your application actually uses. This includes excluding backup tables, staging tables, legacy objects, etc.
- Use the include object types flags (e.g.
<includeRoutines/>,<includePackages/>,<includeUDTs/>,<includeSequences/>,<includeIndexes/>,<includeCheckConstraints/>) to exclude entire object types. Indexes and check constraints, for example, are only needed if you use the generated meta data, e.g. to generate DDL. - Turn off global object references such as
<globalObjectReferences/>, if you always dereference objects from their schema or table classes, rather than from the generatedTables,Keys,Indexes,Routines, etc. classes.
Optional artefacts that are turned off by default
A few flags are turned off by default, but may have been enabled in your configuration. Revisit these if code size is a concern:
-
<pojos/>,<interfaces/>,<daos/>: Each of these generates an additional class per table (and UDT). Note that DAOs also require records to be generated. -
<fluentSetters/>: This generates additional methods or code per column. -
Annotations, including
<jpaAnnotations/>,<validationAnnotations/>(see the section about annotations).
Records
Generated records (<records/>) are mainly needed for CRUD with UpdatableRecords, and to get a type safe TableRecord type when fetching from a single table. If records are turned off, generated tables reference org.jooq.Record as their record type, instead.
The <recordsImplementingRecordN/> flag is turned on by default. It makes generated records implement Record1<T1> to Record22<T1, ..., T22> and generates a lot of additional methods on records and tables, including fieldsRow(), valuesRow(), value1() to value22(), and values(...). The many generic type parameters are particularly heavy for compilers. Without this, you can still use records in the same way, except that the degree and column types are not available to the compiler on the record type.
Implicit join paths
For every foreign key, the code generator produces implicit join path constructors (methods and possibly cached instances) on the referencing table. In schemas with many foreign keys, this can add up to a significant amount of code. The relevant flag is:
-
<implicitJoinPathsToOne/>: Path constructors for outgoing foreign keys (to-one relationships), e.g.BOOK.author()
This is mere convenience. When turned off, you can no longer use implicit path joins, but you can still write explicit joins, including joins using ON KEY, which only depend on the generated keys (<relations/>).
Javadoc, comments, and sources
-
<javadoc/>and<comments/>(and the more fine grained<commentsOnXYZ/>flags): Javadoc doesn't contribute to byte code, but it makes source files larger, which affects compilation, IDE indexing, and version control.
Other flags
-
<deprecated/>: Turn off the generation of deprecated code that is only kept for backwards compatibility. This flag is turned on by default. -
<varargSetters/>and<generatedAnnotation/>: These flags are turned on by default. Vararg setters generate additional methods per array column, and the@Generatedannotation is added to every generated class. -
<indexes/>: Index meta data is only needed if you work with the generated meta data, e.g. to export DDL. -
<relations/>: As a last resort, primary keys, unique keys, and foreign keys can be turned off. This is not recommended, as a lot of functionality depends on them, including UpdatableRecord CRUD, ON KEY joins, and all implicit join paths.
Example
The following configuration turns off all of the convenience features that can be turned off without losing too much essential functionality:
<configuration>
<generator>
<generate>
<!-- Turn off records implementing RecordN (turned on by default) -->
<recordsImplementingRecordN>false</recordsImplementingRecordN>
<!-- Turn off implicit join path convenience -->
<implicitJoinPathsToOne>false</implicitJoinPathsToOne>
<!-- Turn off additional documentation and sources -->
<comments>false</comments>
<deprecated>false</deprecated>
</generate>
</generator>
</configuration>
See the configuration XSD, standalone code generation, and maven code generation for more details.
new org.jooq.meta.jaxb.Configuration()
.withGenerator(
new Generate()
// Turn off records implementing RecordN (turned on by default)
.withRecordsImplementingRecordN(false)
// Turn off implicit join path convenience
.withImplicitJoinPathsToOne(false)
// Turn off additional documentation and sources
.withComments(false)
.withDeprecated(false)
)
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 {
generator {
generate {
// Turn off records implementing RecordN (turned on by default)
recordsImplementingRecordN = false
// Turn off implicit join path convenience
implicitJoinPathsToOne = false
// Turn off additional documentation and sources
comments = false
deprecated = false
}
}
}
See the configuration XSD and gradle code generation for more details.
Whether turning off any of these flags helps depends on your schema and your build. It is advisable to measure compilation time and memory consumption of your build before and after such changes, and to turn off only those features that make a noticeable difference.
Feedback
Do you have any feedback about this page? We'd love to hear it!