#1212 - ENH: Add query findSingleAttribute()

This commit is contained in:
Rob Bygrave
2017-11-18 22:36:08 +13:00
parent d5555ed9a9
commit fcff571664
4 changed files with 132 additions and 65 deletions
+19 -1
View File
@@ -5,9 +5,9 @@ import io.ebean.search.MultiMatch;
import io.ebean.search.TextCommonTerms;
import io.ebean.search.TextQueryString;
import io.ebean.search.TextSimple;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.NonUniqueResultException;
import java.sql.Timestamp;
import java.util.Collection;
@@ -261,6 +261,24 @@ public interface ExpressionList<T> {
@Nonnull
<A> List<A> findSingleAttributeList();
/**
* Execute a query returning a single value of a single property/column.
* <p>
* <pre>{@code
*
* String name =
* Ebean.find(Customer.class)
* .select("name")
* .where().eq("id", 42)
* .findSingleAttribute();
*
* }</pre>
*/
default <A> A findSingleAttribute() {
List<A> list = findSingleAttributeList();
return !list.isEmpty() ? list.get(0) : null;
}
/**
* Execute the query returning a single bean or null (if no matching
* bean is found).
+15
View File
@@ -813,6 +813,21 @@ public interface Query<T> {
@Nonnull
<A> List<A> findSingleAttributeList();
/**
* Execute a query returning a single value of a single property/column.
* <p>
* <pre>{@code
*
* String name =
* Ebean.find(Customer.class)
* .select("name")
* .where().eq("id", 42)
* .findSingleAttribute();
*
* }</pre>
*/
<A> A findSingleAttribute();
/**
* Execute the query returning either a single bean or null (if no matching
* bean is found).
@@ -1284,6 +1284,12 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return (List<A>) server.findSingleAttributeList(this, null);
}
@Override
public <A> A findSingleAttribute() {
List<A> list = findSingleAttributeList();
return !list.isEmpty() ? list.get(0) : null;
}
@Override
public T findOne() {
return server.findOne(this, null);