findSingleAttribute

Execute a query retrieving the value of a single column.

To retrieve the value from a single property of a specific model, we use findSingleAttribute. We need to use the select() method to select which column to retrieve the value for.

Example: single property

var cust = QCustomer.alias();

String name = new QCustomer()
    .select(cust.name)
    .id.equalTo(42)
    .findSingleAttribute();

This translates to an SQL query that only has the single property in the select clause, like the following:

select t0.name from customers t0 where t0.id = 42

Example: sql function

List<String> names =
  new QContact()
    .select("concat(lastName,', ',firstName)")
    .lastName.startsWith("A")
    .findSingleAttributeList();

Casting result type

When using sql functions sometimes we need to use a ::type to denote what logical type we are going to get back via JDBC. In the example below we cast the sql function result via ::BigDecimal such that Ebean will read the result of the function via JDBC as a BigDecimal.

The type is the short name of any scalar type that Ebean supports. Most commonly this would be BigDecimal, Long, String but any scalar type that Ebean supports can be used.

Example: sql function

// given route is a Postgis geometry(linestring,4326)
// return the distance between the start and end points

BigDecimal routeDistance =
  new QTrip()
    .select("ST_Distance(ST_StartPoint(route), ST_EndPoint(route))::BigDecimal")
    .id.eq(tripId)
    .findSingleAttribute();

The above uses ::BigDecimal such that the result of the sql function is read as a BigDecimal.

findSingleAttributeList

The same as findSingleAttribute but returning a list of results.

Example

var cust = QCustomer.alias();

List<String> names
  = new QCustomer()
    .setDistinct(true)
    .select(cust.name)
    .order()
      .name.asc()
    .findSingleAttributeList();