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.

Examples:
Contact contact =
  new QContact()                               // Contact query bean
    .email.equalTo("rob@foo.com")              // type safe expression with IDE auto-completion
    .findOne();
List<Customer> customers =
  new QCustomer()                              // Customer query bean
    .status.equalTo(Status.NEW)
    .billingAddress.city.equalTo("Auckland")
    .contacts.isEmpty()
    .findList();

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.

Enhancement

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.

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

Caveats

Refactor rename entity bean

When we refactor rename an entity bean in an IDE the query beans we should trigger a build all. This is because the query beans for the old bean names are kept around until the next build all.

Once we do a build all this will effectively delete the old query beans and generate the new ones. At this point we get compiler errors for the queries that we need to adjust (use the new query bean names).

Edit Page