public interface HqlQuery
Modifier and Type | Method and Description |
---|---|
void |
delete()
Surrogate for update used for delete queries.
|
HqlQuery |
limit(int rowsCount)
Set the maximum number of rows this query may return.
|
HqlQuery |
limit(int offset,
int rowsCount)
Set first record offset and maximum number of rows this query may return.
|
<T> List<T> |
list()
Execute database select and return list of entities.
|
<T> List<T> |
list(Class<T> type)
Variant of
list() that return list of entities of specified type. |
HqlQuery |
load(String... lazyFields)
Load fields declared as lazy into mapping definition.
|
<K,V> Map<K,V> |
map()
Execute database select on two properties and returns them as key/values map, first property in query being the map
key.
|
<T> T |
object()
Execute database select and return single entity.
|
HqlQuery |
param(String name,
Collection<?> value,
boolean forceEmpty)
Bind collection to named query parameter and select behavior for empty value.
|
HqlQuery |
param(String name,
Object value)
Bind a not null value to a named query parameter.
|
void |
update()
Execute update designated by this HQL query.
|
HqlQuery param(String name, Object value)
name
- parameter name,value
- parameter value.IllegalArgumentException
- if parameter value is null.HqlQuery param(String name, Collection<?> value, boolean forceEmpty)
param(String, Object)
.
The third argument is used to select this HQL query behavior when value
argument is an empty
collection. Not sure if depends on database driver or is by specification, but observed on some: there is exception
when use empty collection parameter with IN
clause. If true, argument forceEmpty
will
force list()
and list(Class)
to return empty result; otherwise provided collection should not be
empty.
name
- parameter name,value
- parameter value,forceEmpty
- force list()
and list(Class)
to return empty result if value
is
empty.IllegalArgumentException
- if parameter value is null.HqlQuery limit(int rowsCount)
Query.setMaxResults(int)
.rowsCount
- maximum number of rows count.IllegalArgumentException
- if rows count argument is not strict positive.HqlQuery limit(int offset, int rowsCount)
Query.setFirstResult(int)
and Query.setMaxResults(int)
.offset
- first record offset, starting with 0,rowsCount
- the number of maximum rows count.IllegalArgumentException
- if any given argument is negative or rows count is zero.HqlQuery load(String... lazyFields)
Hibernate.initialize(Object)
.lazyFields
- variable number of field names.IllegalArgumentException
- if no lazy field argument supplied.<T> T object()
class Person { int id; String name; } . . . Person person = sm.HQL("from Person where id=?", personId).object();In above, Person class has mapping defined and entity has the same name. Query must return at most one record into results set; there is Hibernate runtime exception if more records. This method uses internally
Query.uniqueResult()
.T
- instance type.org.hibernate.NonUniqueObjectException
- if query returns more than one record.org.hibernate.HibernateException
- for Hibernate related fails like bad query syntax or entity not defined.<T> List<T> list()
object()
for a discussion about entities. This
method uses internally Query.list()
. Usage pattern is like below:
List<Person> persons = sm.HQL("from Person").list();
T
- list type.org.hibernate.HibernateException
- for Hibernate related fails like bad query syntax or entity not defined.<T> List<T> list(Class<T> type)
list()
that return list of entities of specified type. This variant is especially useful when
want to map columns to arbitrary user defined types. Selected column alias is used to invoke setter on user defined
type. Column alias should be present even if the same as column name; it is possible for database driver to force
column names as upper case if if in HWL query is lower.
Usage pattern is like below:
class Person { private String name; private int age; ... // properties setters } List<Person> persons = sm.HQL("select column1 as name, column2 as age from ... ").list(Person.class);Implementation uses Transformers.aliasToBean(type) to bind a built-in result transformer. It is caller responsibility to ensure type compatibility between selected columns and type fields. For example if database numeric value is LONG there will be exception when try to set
age
value.T
- list type.type
- requested entity type.<K,V> Map<K,V> map()
Map<String, Integer> serialMap = sm.HQL("select serial,id from Ticket where county=?", county).map();Note that in above example database ID is map value, not necessarily map key. Key and value are determined by property position in query, first is key and the second is value.
K
- map key type.V
- map value type.org.hibernate.HibernateException
- for Hibernate related fails like bad query syntax or entity not defined.void update()
void delete()
update()
.Copyright © 2019. All rights reserved.