Query beans

Query beans are optional but they provide a nice way to write queries with type safe compile time checking. They are also easy to use and learn with IDE auto-completion on properties and expressions.

Query beans are generated using a Java annotation processing (APT) or Kotlin annotation processing (KAPT).

For each entity a query bean is generated with the same name but prefixed with Q. So for an entity bean called Customer there is a query bean generated called QCustomer.

When the entity bean model changes the query beans are regenerated and as developers we get compile time checking on our application queries - we get a compile time error if our queries are no longer valid for the model.

Contact contact =
  new QContact()                               // Contact query bean
    .email.equalTo("rob@foo.com")              // type safe expression with IDE auto-completion
List<Customer> customers =
  new QCustomer()                              // Customer query bean

APT / KAPT (generating query beans)

To generate Java query beans we use the io.ebean : querybean-generator java annotation processor and to generate Kotlin query beans we use io.ebean : kotlin-querybean-generator.

Generating with Maven

Refer to docs / getting-started / maven query beans for details of how to generate query beans using maven.

Generating with Gradle

Refer to docs / getting-started / gradle query beans for details of how to generate query beans using gradle.


Query beans require enhancement to work. In the src/main/resources/ebean.mf manifest file we need to specify via querybean-packages the packages that should be enhanced for query beans.

Note that for Java we additionally need to enhance the code that calls the query ebeans. For Kotlin we only need to enhance the query beans themselves.

entity-packages: org.example.domain
transactional-packages: org.example
querybean-packages: org.example

Edit Page