Skip to main content

Generate Code

Code Generation

Similar to JPA2.0 Criteria, QueryDsl, Fluent MyBatis, etc., Jimmer uses strongly typed DSLs to catch errors at compile-time rather than runtime.

So some additional code needs to be generated at compile-time based on user-defined entities:

  • Java users use Annotation Processor
  • Kotlin users use KSP
pom.xml
...other code omitted... 

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.babyfish.jimmer</groupId>
<artifactId>jimmer-apt</artifactId>
<version>${jimmer.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

...other code omitted...
info
  • The first three: Standard configurations
  • The last two: Community-provided Gradle plugin for further simplifying configurations
note

KSP only supports gradle, It has been proven that KSP's third-party Maven plug-in support cannot keep up with the version iteration of 'kotlin/KSP' itself, and often encounters many problems during the upgrade process.

Eventually, Jimmer dropped Maven support for Kotlin and asked Kotlin developers to use Gradle.

After this configuration, running Maven or Gradle will generate additional source code based on user-defined entities.

caution

The above only applies to simple projects without separation.

For real projects with separate entity definition, generator (annotation processor/ksp) must be configured in the project that defines entity types.

Jimmer data types require one of:

  • @Immutable
  • @Entity
  • @MappedSuperclass
  • @Embeddable

The first is ORM-agnostic, the others are ORM-related.

If entities use the ORM annotations (eg: @Entity), the generated code requires jimmer-sql while entity code itself only needs jimmer-core:

pom.xml
...other code omitted...

<dependencies>

<!-- User entity code -->
<dependency>
<groupId>org.babyfish.jimmer</groupId>
<artifactId>jimmer-core</artifactId>
<version>${jimmer.version}</version>
</dependency>

<!-- Generated code -->
<dependency>
<groupId>org.babyfish.jimmer</groupId>
<artifactId>jimmer-sql</artifactId>
<version>${jimmer.version}</version>
<!-- Imported by other projects -->
<scope>provided</scope>
</dependency>

...other dependencies omitted...
</dependencies>

...other code omitted...

Verify Successful Generation

If generation succeeded, developers should see:

JavaKotlin
note

Screenshots are from Gradle project.

For Maven, top level directory is target instead of build.

caution

Normally IDE marks generated code directories with icon.

But sometimes IntelliJ fails to do so, preventing use of generated code.

In this case, right-click on directory, choose Mark Directory As -> Generated Sources Root.

Generated code includes:

JavaKotlinDescription
XXXDraft.javaXXXDraft.ktMutable proxy interface for immutable object, object implementation, and methods to create and "modify" objects
XXXProps.javaXXXProps.ktTyped SQL DSL
XXXTable.java
XXXTableEx.java
XXXFetcher.javaXXXFetcher.ktObject fetcher DSL defining shape of complex query results