Yawn is a Kotlin ORM-wrapper that provides a type-safe, expressive, Criteria-style query syntax using custom KSP-generated entity metadata.
Yawn is a thin encapsulation of the Hibernate Criteria Query API into a Kotlin-friendly, type-safe and intuitive interface.
It leverages KSP to generate type-safe definitions used to power Yawn Queries.
[!NOTE] 💡 Want to take a peek at a complete example we made of using Yawn in a multi-module Gradle project? Check our example repo.
There are two sets of dependencies you must add to your project to use Yawn.
The yawn-processor dependency is what generates the definitions for your entities annotated with @YawnEntity.
That needs to be added as a compileOnly and also ksp dependency:
compileOnly("com.faire.yawn:yawn-processor:$version")
ksp("com.faire.yawn:yawn-processor:$version")
As an alternative, you can use the Yawn Gradle plugin to automatically add the processor for you - see the Yawn Gradle Plugin readme for more details. This is recommended for multi-module Gradle projects.
The yawn-api as a regular dependency in order to actually make queries:
implementation("com.faire.yawn:yawn-api:$version")
Annotate your Hibernate entities with @YawnEntity in order to have the necessary table and column definitions generated for them.
@Entity
@Table(name = "books")
@YawnEntity // <-- add this
class Book {
// ...
}
In order to hook Yawn into your Hibernate setup, you need to provide a QueryFactory implementation that knows how map the Yawn models into a Hibernate query.
For inspiration, you can check out the YawnTestQueryFactory implementation.
Tip: wrap the Yawn class creation within your transaction management code to make it easier to use throughout your codebase!
Finally, you are ready! Now you can write your type-safe queries using the power of Yawn:
val yawn = Yawn(queryFactory = YourQueryFactory(...))
val tolkienBooks = yawn.query(BookTable) { books ->
val authors = join(books.author)
addEq(authors.name, "J. R. R. Tolkien")
}.list()
For more advanced details, read through our docs!
No longer guess column names and types. Begone brittle strings. No runtime errors when running your tests (or even worse, in prod).
With Yawn, it is impossible to pass in the wrong type, enforced by our friend the compiler; join paths are guaranteed to be correct; project to data classes with ease.
Easy to refactor, investigate, and safely delete fields. Analyze cascading impacts of your changes with ease.
Leverage Kotlin’s powerful introspection and refactor tools like you already do with everything else.
Use the power of the IDE that we know and love, now for queries.
Get code suggestions and hints from your coding environment.
Yawn keeps mostly the same syntax provided by Hibernate Criteria API, just adding type-safety as the cherry on top.
If you like that style of building queries, but want some type-safety on top, Yawn might just be for you.
Yawn is just a thin wrapper on top of Hibernate queries. In order to power type-safe queries, Yawn generates objects representing the metadata of each
table and column related to a Hibernate entity annotated with @YawnEntity; so the migration can be completely unobtrusive and opt-in.
The generation is powered by KSP, and is composed by a set of TableDef (table definitions) that contain a set
of ColumnDef (column definitions). The generate code contains the type information as generics that is necessary to make the queries safe. For example, it
forces you to query columns that belong (or were joined from) to the table being queried; and it enforces the type of the values provided for comparison.
If you like Yawn, give us a star to help the project!
Have you found a bug or have a suggestion? Open an issue and we will take a look at it as soon as possible.
Do you want to contribute with a PR? Make sure to read our Contributing Guide!
Two-phase pagination should always GROUP BY the unique column in the page-of-keys query
https://github.com/Faire/yawn/issues/161
Labels: None
Support COALESCE / first-non-null column expressions in ORDER BY for pagination
https://github.com/Faire/yawn/issues/136
Labels: migration-path
Support distinct root entity for collection joins on entity queries
https://github.com/Faire/yawn/issues/135
Labels: migration-path
Support custom ORDER BY expressions in Yawn pagination
https://github.com/Faire/yawn/issues/133
Labels: migration-path
Note: If these contribution guidelines are not followed your issue or PR might be closed, so please read these instructions carefully.
Fixes #xxxx in your PR text, where xxxx is the issue number (if there is one).For a contribution to be accepted:
./gradlew build (or your IDE of choice);feat:, fix:, etc).If the contribution doesn't meet these criteria, a maintainer will discuss it with you on the issue or PR. You can still continue to add more commits to the branch you have sent the Pull Request from and it will be automatically reflected in the PR.
git clone git@github.com:<YOUR_GITHUB_USER>/yawn.git).Yawn is managed with the included Gradle wrapper, which should download the appropriate version for you. You will also need Java 21 or newer.
To run the "build", which includes compilation, tests, lint, formatting, and other checks, run:
./gradlew build
If you want to run the spellchecker locally, you will have to install cspell; you can do so using npm or yarn:
npm install -g cspell
Then you can run it with the following arguments:
./scripts/cspell-run.sh
If you want to lint the markdown files you have to install markdownlint-cli; once that is installed you can
run scripts/markdownlint-run.sh to check if the markdown follows the rules.
Note that, sadly, a particularly laborious rule, MD013, does not provide an auto-fix option. However,
you can use other tools to circumvent this. For example, the extension Rewrap for VSCode, when
configured with rewrap.wrappingColumn=160, will do the trick for you.
We use prettier for YAML formatting and actionlint for GitHub Actions workflow validation.
To run YAML linting locally:
# Check YAML files
scripts/yaml-lint-run.sh
# Check and auto-fix YAML formatting issues
scripts/yaml-lint-run.sh --fix
The script will automatically check for and install the required tools (prettier and actionlint) if they're not already available.
The prettier configuration is defined in .github/yaml-lint/.prettierrc.yml.
main (e.g. git checkout -b my-new-feature)When doing breaking changes, a deprecation tag should be added first containing a message that conveys to what method should be used instead to perform the task.
Also don't forget to include the ! as part of your conventional commit prefix when actually removing old code.
Go to the pull request page of Yawn and in the top of the page it will ask you if you want to open a pull request from your newly created branch.
The title of the pull request should start with a [conventional commit][conventional commit] type.
Allowed types are:
fix: -- patches a bug and is not a new feature;feat: -- introduces a new feature;docs: -- updates or adds docum