Skip to content
Permalink
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
<html>
<head>
<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">
</head>
<body>
<h2 id="querybeans">Query beans</h2>
<p>
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.
</p>
<p>
Query beans are generated using a Java annotation processing (<em>APT</em>) or
Kotlin annotation processing (<em>KAPT</em>).
</p>
<p>
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>.
</p>
<p>
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.
</p>
<h5>Examples:</h5>
```java
Contact contact =
new QContact() // Contact query bean
.email.equalTo("rob@foo.com") // type safe expression with IDE auto-completion
.findOne();
```
```java
List<|Customer> customers =
new QCustomer() // Customer query bean
.status.equalTo(Status.NEW)
.billingAddress.city.equalTo("Auckland")
.contacts.isEmpty()
.findList();
```
<h2 id="apt" class="art">APT / KAPT <em>(generating query beans)</em></h2>
<p>
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>.
</p>
<h4 id="maven" class="art">Generating with Maven</h4>
<p>
Refer to <a href="/docs/getting-started/maven">docs / getting-started / maven</a>
for details of how to generate query beans using maven.
</p>
<h4 id="gradle">Generating with Gradle</h4>
<p>
Refer to <a href="/docs/getting-started/gradle">docs / getting-started / gradle</a>
for details of how to generate query beans using gradle.
</p>
<h2 id="enhancement" class="art">Enhancement</h2>
<p>
Query beans require enhancement to work. In the <code>src/main/resources/ebean.mf</code> manifest file
we need to specify via <code>querybean-packages</code> the packages that should be enhanced
for query beans.
</p>
<p>
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.
</p>
<h5>Example</h5>
```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>
<p>
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>.
</p>
<p>
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).
</p>
<@next "Where" "/docs/query/where"/>
<@edit "/docs/query/query-beans"/>
</body>
</html>
You can’t perform that action at this time.