Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
<title>Query beans | Ebean</title>
<meta name="layout" content="_layout2/base-docs.html"/>
<meta name="bread1" content="Query" href="/docs/query/"/>
<meta name="bread2" content="Query beans" href="/docs/query/query-beans"/>
<#assign n0_docs="true">
<#assign n1_query="true">
<#assign querybeans="true">
<h2 id="querybeans">Query beans</h2>
Query beans are optional but they provide a nice way to write queries with <code>type safe
compile time checking</code>. They are also easy to use and learn with <code>IDE auto-completion</code>
on properties and expressions.
Query beans are generated using a Java annotation processing (<em>APT</em>) or
Kotlin annotation processing (<em>KAPT</em>).
For each entity a <em>query bean</em> is generated with the same name but prefixed with <code>Q</code>.
So for an entity bean called <em>Customer</em> there is a query bean generated called <em>QCustomer</em>.
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.
<pre content="java">
Contact contact =
new QContact() // Contact query bean
.email.equalTo("") // type safe expression
.findOne(); // with IDE auto-completion
<pre content="java">
List<|Customer> customers =
new QCustomer() // Customer query bean
.status.equalTo(Status.NEW)"Auckland") // joins automatically added
.contacts.isEmpty() // to support expressions
<h2 id="apt" class="art">APT / KAPT <em>(generating query beans)</em></h2>
To generate <code>Java</code> query beans we use the <em>io.ebean:querybean-generator</em>
java annotation processor and to generate <code>Kotlin</code> query beans we use
<h4 id="maven" class="art">Generating with Maven</h4>
Refer to <a href="/docs/getting-started/maven">docs / getting-started / maven</a>
for details of how to generate query beans using maven.
<h4 id="gradle">Generating with Gradle</h4>
Refer to <a href="/docs/getting-started/gradle">docs / getting-started / gradle</a>
for details of how to generate query beans using gradle.
<h2 id="enhancement" class="art">Enhancement</h2>
Note that prior to version 12.1.8 we need to edit a <code>src/main/resources/</code>
manifest file to specify <code>querybean-packages</code> for the packages that should
be enhanced for query beans. This is no longer needed from 12.1.8 onwards.
<h5>Example (Pre 12.1.8)</h5>
<pre content="properties">
entity-packages: org.example.domain
transactional-packages: org.example
querybean-packages: org.example
<h2 id="caveats" class="art">Caveats</h2>
<h4>Refactor rename entity bean</h4>
When we refactor rename an entity bean in an IDE the query beans we should trigger a <code>build all</code>.
This is because the query beans for the old bean names are kept around until the next <code>build all</code>.
Once we do a <code>build all</code> 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).
<h2 id="groovy">Groovy Spock</h2>
When using query beans with Groovy perhaps in Spock tests we need to use them inside a <code>@CompileStatic</code>
<pre content="groovy">
static def findCount(String name) {
return new QDbSample().name.eq(name).findCount()
Then we can use the helper method in our spock test like:
<pre content="groovy">
// this line NPEs
findCount('fred') == 1
<@next_edit "Where" "/docs/query/where" "/docs/query/query-beans.html"/>