Modifier and Type | Field and Description |
---|---|
private boolean |
forceEmpty
Flag to force
list() or list(Class) return empty result. |
private String |
hql
Hibernate query.
|
private String[] |
lazyFields
Name for entity fields that should be explicitly loaded.
|
private Map<String,Object> |
namedParameters
Named parameters in no particular order.
|
private int |
offset
First result offset.
|
private Object[] |
positionedParameters
Positioned parameters into order declared by Hibernate query.
|
private int |
rowsCount
Maximum allowed number of items into results set.
|
private org.hibernate.Session |
session
Hibernate session.
|
Constructor and Description |
---|
HqlQueryImpl(org.hibernate.Session session,
String hql,
Object... positionedParameters)
Create Hibernate query with optional indexed parameters.
|
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
HqlQuery.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.
|
private org.hibernate.Query |
query(String hql,
Class<?>... type)
Create Hibernate query object and initialize it from this helper properties.
|
void |
update()
Execute update designated by this HQL query.
|
private org.hibernate.Session session
private String hql
private Object[] positionedParameters
private String[] lazyFields
private int offset
private int rowsCount
private boolean forceEmpty
list()
or list(Class)
return empty result. It is set to true by
param(String, Collection, boolean)
when provided parameter value is empty.public HqlQueryImpl(org.hibernate.Session session, String hql, Object... positionedParameters)
session
- Hibernate session,hql
- Hibernate query,positionedParameters
- variable number of positioned parameters.public HqlQuery param(String name, Object value)
HqlQuery
public HqlQuery param(String name, Collection<?> value, boolean forceEmpty)
HqlQuery
HqlQuery.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 HqlQuery.list()
and HqlQuery.list(Class)
to return empty result; otherwise provided collection should not be
empty.
param
in interface HqlQuery
name
- parameter name,value
- parameter value,forceEmpty
- force HqlQuery.list()
and HqlQuery.list(Class)
to return empty result if value
is
empty.public HqlQuery limit(int rowsCount)
HqlQuery
Query.setMaxResults(int)
.public HqlQuery limit(int offset, int rowsCount)
HqlQuery
Query.setFirstResult(int)
and Query.setMaxResults(int)
.public HqlQuery load(String... lazyFields)
HqlQuery
Hibernate.initialize(Object)
.public <T> List<T> list()
HqlQuery
HqlQuery.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();
public <T> List<T> list(Class<T> type)
HqlQuery
HqlQuery.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.public <K,V> Map<K,V> map()
HqlQuery
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.
public <T> T object()
HqlQuery
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()
.public void update()
HqlQuery
public void delete()
HqlQuery
HqlQuery.update()
.Copyright © 2019. All rights reserved.