Skip to content
This repository was archived by the owner on Oct 13, 2020. It is now read-only.

Commit

Permalink
Support 8.2.1 API changes - findSingleAttributeList(), findIterate()
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Aug 5, 2016
1 parent 3cc5d28 commit 24a083b
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.avaje.ebean</groupId>
<artifactId>ebean-querybean</artifactId>
<version>8.1.2-SNAPSHOT</version>
<version>8.2.1-SNAPSHOT</version>

<parent>
<groupId>org.avaje</groupId>
Expand All @@ -25,7 +25,7 @@
<dependency>
<groupId>org.avaje.ebean</groupId>
<artifactId>ebean</artifactId>
<version>8.1.1</version>
<version>8.2.1</version>
<scope>provided</scope>
</dependency>

Expand Down
67 changes: 62 additions & 5 deletions src/main/java/org/avaje/ebean/typequery/TQRootBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ public List<T> findList() {
*
* @see EbeanServer#findIds(Query, Transaction)
*/
public List<Object> findIds() {
public <A> List<A> findIds() {
return query.findIds();
}

Expand All @@ -1165,15 +1165,72 @@ public List<Object> findIds() {
*
* @see EbeanServer#findMap(Query, Transaction)
*/
public Map<?, T> findMap() {
public <K> Map<K, T> findMap() {
return query.findMap();
}

/**
* Return a typed map specifying the key property and type.
* Execute the query iterating over the results.
* <p>
* Note that findIterate (and findEach and findEachWhile) uses a "per graph"
* persistence context scope and adjusts jdbc fetch buffer size for large
* queries. As such it is better to use findList for small queries.
* </p>
* <p>
* Remember that with {@link QueryIterator} you must call {@link QueryIterator#close()}
* when you have finished iterating the results (typically in a finally block).
* </p>
* <p>
* findEach() and findEachWhile() are preferred to findIterate() as they ensure
* the jdbc statement and resultSet are closed at the end of the iteration.
* </p>
* <p>
* This query will execute against the EbeanServer that was used to create it.
* </p>
* <pre>{@code
*
* Query<Customer> query =
* new QCustomer()
* .status.equalTo(Customer.Status.NEW)
* .order()
* id.asc()
* .query();
*
* QueryIterator<Customer> it = query.findIterate();
* try {
* while (it.hasNext()) {
* Customer customer = it.next();
* // do something with customer ...
* }
* } finally {
* // close the underlying resources
* it.close();
* }
*
* }</pre>
*/
public QueryIterator<T> findIterate() {
return query.findIterate();
}

/**
* Execute the query returning a list of values for a single property.
*
* <h3>Example</h3>
* <pre>{@code
*
* List<String> names =
* new QCustomer()
* .setDistinct(true)
* .select(name)
* .findSingleAttributeList();
*
* }</pre>
*
* @return the list of values for the selected property
*/
public <K> Map<K, T> findMap(String keyProperty, Class<K> keyType) {
return query.findMap(keyProperty, keyType);
public <A> List<A> findSingleAttributeList() {
return query.findSingleAttributeList();
}

/**
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/org/querytest/QCustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,69 @@

import com.avaje.ebean.PagedList;
import com.avaje.ebean.Query;
import com.avaje.ebean.QueryIterator;
import org.example.domain.Customer;
import org.example.domain.typequery.QContact;
import org.example.domain.typequery.QCustomer;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class QCustomerTest {

@Test
public void findSingleAttribute() {

List<String> names = new QCustomer()
.setDistinct(true)
.select(QCustomer.alias().name)
.status.equalTo(Customer.Status.BAD)
.findSingleAttributeList();

assertThat(names).isNotNull();
}

@Test
public void findIterate() {

Customer cust = new Customer();
cust.setName("foo");
cust.setStatus(Customer.Status.GOOD);
cust.save();

List<Long> ids = new QCustomer()
.status.equalTo(Customer.Status.GOOD)
.findIds();

assertThat(ids).isNotEmpty();


Map<List, Customer> map = new QCustomer()
.status.equalTo(Customer.Status.GOOD)
.findMap();

assertThat(map.size()).isEqualTo(ids.size());

QueryIterator<Customer> iterate = new QCustomer()
.status.equalTo(Customer.Status.GOOD)
.findIterate();

try {
while (iterate.hasNext()) {
Customer customer = iterate.next();
customer.getName();
}
} finally {
iterate.close();
}
}


@Test
public void isEmpty() {

Expand Down

0 comments on commit 24a083b

Please sign in to comment.