Critter
Critter will look at your source code and generate type safe criteria builders for each model object. It will also, by default, generate model and codec code specific to your entities removing the need for most reflections at run time. Morphia 2.3+ will automatically import these.
What difference does it make?
Before critter, your criteria might look something like this:
Query<Book> query = ds.find(Book.class)
.filter(and(
eq("bookmark", bookmark),
eq("database", database)));
But using critter, it would look like this:
Query<Book> query = ds.find(Book.class)
.filter(and(
Book.bookmark().eq(bookmark),
Book.database().eq(database)));
Notice how bookmark() and database() methods were created based on the model object Book’s fields. The comparison methods you’re familiar with from Morphia’s filters API are all there but now only take the type of the field itself. With this code in place if the model object changes, the code above runs the risk of failing to compile allowing you to catch model/query conflicts at compile time rather than waiting for things to fail at runtime (or in your tests if you’re lucky enough to have those).
You can see working examples in the [tests](https://github.com/MorphiaOrg/critter/tree/master/tests).
Building with critter
Critter supports a number of configuration options that can be passed to the maven or gradle plugins depending on your build.
Option | Property | Description |
---|---|---|
criteriaPackage |
critter.package |
If this option is omitted, the code will be generated using the
package of your entities with |
generateCodecs |
critter.codecs |
Turns codec generation on or off. Possible values: true, false. Defaults to true. |
outputType |
critter.type |
Setting this changes the language used in the generated criteria classes. Possible values: java, kotlin |
Java
To use it with your java projects, you simply need to add a plugin to your build file. To use critter with maven, you would update your pom as shown here:
<plugin>
<groupId>dev.morphia.critter</groupId>
<artifactId>critter-maven</artifactId>
<version>4.4.3</version>
<executions>
<execution>
<id>critter</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<criteriaPackage>com.bob.bar</criteriaPackage>
</configuration>
</plugin>
This will generate your criteria classes in target/generated-sources/critter
and add the directory as a source directory of
your maven project.
If you use gradle, you would configure your file like so (using the kotlin dsl):
plugins {
id("dev.morphia.critter") version "4.4.3"
}
This will generate your criteria classes in build/generated-src/critter
and add the directory as a source directory of your gradle
project.
Kotlin
To build your kotlin code in maven, it’s a slightly different approach. The Kotlin support is built on KSP so it requires a few
additions to the kotlin-maven-plugin
setup:
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
...
<configuration>
<compilerPlugins>
<compilerPlugin>ksp</compilerPlugin>
</compilerPlugins>[1]
</configuration>
<dependencies>
<dependency>
<groupId>com.dyescape</groupId>
<artifactId>kotlin-maven-symbol-processing</artifactId>
<version>1.5</version>[2]
</dependency>
<dependency>
<groupId>dev.morphia.critter</groupId>
<artifactId>critter-maven</artifactId>
<version>4.4.3</version>[3]
</dependency>
</dependencies>
</plugin>
Gradle support similarly needs some configuration for KSP. Add the following line to the plugins
section of your gradle build:
id("com.google.devtools.ksp") version "1.8.10-1.0.9"
With that done, one new addition to your dependencies
and you’re all set:
ksp("dev.morphia.critter:dev.morphia.critter.gradle.plugin:4.4.3")
Issues and support
Please check here if you find an issue or need help.