Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
110 lines (89 sloc) 3.59 KB
<title>Query beans | Ebean</title>
<meta name="layout" content="_layout/docs_query_select.html"/>
<meta name="bread2" content="Query beans" href="/docs/query/query-beans"/>
<#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.
Contact contact =
new QContact() // Contact query bean
.email.equalTo("") // type safe expression with IDE auto-completion
List<|Customer> customers =
new QCustomer() // Customer query bean
<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
<em>io.ebean : kotlin-querybean-generator</em>.
<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>
Query beans require enhancement to work. In the <code>src/main/resources/</code> manifest file
we need to specify via <code>querybean-packages</code> 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
<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).
<@next "Where" "/docs/query/where"/>
<@edit "/docs/query/query-beans"/>
You can’t perform that action at this time.