Modifier and Type | Field and Description |
---|---|
private Map<String,Object> |
namedParameters
Named parameters in no particular order.
|
private int |
offset
First result offset.
|
private Object[] |
positionedParameters
Indexed parameters into order declared by SQL query.
|
private int |
rowsCount
Maximum allowed number of items into results set.
|
private Map<String,org.hibernate.type.Type> |
scalars
Map of Java type associated with returned column name.
|
private org.hibernate.Session |
session
Hibernate session.
|
private String |
sql
SQL query.
|
private static Map<Class<?>,org.hibernate.type.Type> |
types
Map of Java class to Hibernate standard basic types.
|
Constructor and Description |
---|
SqlQueryImpl(org.hibernate.Session session,
String sql,
Object... positionedParameters)
Construct SQL query instance.
|
Modifier and Type | Method and Description |
---|---|
void |
delete()
Surrogate for update used for delete queries.
|
SqlQuery |
limit(int rowsCount)
Set the maximum number of rows this query may return.
|
SqlQuery |
limit(int offset,
int rowsCount)
Set first record offset and maximum number of rows this query may return.
|
<T> List<T> |
list(Class<T> entity)
Execute database select and return list of entities.
|
<T> List<T> |
list(String alias,
Class<T> type)
Execute database select and return list of scalar value.
|
<K,V> Map<K,V> |
map()
Execute database select on two columns and returns them as key/values map, first column in query being the map key.
|
<T> T |
object(Class<T> entity)
Execute database select and return single entity.
|
<T> T |
object(String alias,
Class<T> type)
Execute scalar query.
|
<T> T |
object(String alias,
Class<T> type,
Object defaultValue)
Convenient alternative to
SqlQuery.object(String, Class) method returning given default value instead of null. |
SqlQuery |
param(String name,
Object value)
Bind a not null value to a named query parameter.
|
private org.hibernate.SQLQuery |
query(Class<?>... entity)
Create SQL query object and initialize it from this helper properties.
|
private void |
scalar(String alias,
Class<?> type)
Convenient method to convert Java type to Hibernate standard basic type and insert into scalars map.
|
void |
update()
Execute update designated by this SQL query.
|
private org.hibernate.Session session
private String sql
private Object[] positionedParameters
private int offset
private int rowsCount
private Map<String,org.hibernate.type.Type> scalars
public SqlQueryImpl(org.hibernate.Session session, String sql, Object... positionedParameters)
session
- database session,sql
- SQL query,positionedParameters
- variable number of positional parameters.public SqlQuery param(String name, Object value)
SqlQuery
public SqlQuery limit(int rowsCount)
SqlQuery
Query.setMaxResults(int)
.public SqlQuery limit(int offset, int rowsCount)
SqlQuery
Query.setFirstResult(int)
and Query.setMaxResults(int)
.public <T> T object(String alias, Class<T> type)
SqlQuery
In this interface context scalar type is a simple Java class that wrap a single value, that is, is not an aggregate
object or collection, e.g. File
that wraps a file path. Internally uses a hash to bind Java type to
StandardBasicTypes
then delegate SQLQuery.addScalar(String, Type)
.
Integer count = sm.SQL("SELECT COUNT(*) AS count FROM table").object("count", Integer.class);This method returns scalar value or null. If result is to be assigned to a primitive type uses
variant
with default value.public <T> T object(String alias, Class<T> type, Object defaultValue)
SqlQuery
SqlQuery.object(String, Class)
method returning given default value instead of null. This
is especially useful when assign scalar type to primitive Java types.
int count = sm.SQL("SELECT COUNT(*) AS count FROM table").object("count", Integer.class, 0);
public <T> T object(Class<T> entity)
SqlQuery
class Person { int id; String name; } . . . Person person = sm.SQL("SELECT id,name FROM person WHERE id=?", personId).object(Person.class);In above, Person class has mapping defined; not all class fields must be present into mapping definition but all mapped fields, which actually defined the entity, must be present into query result set. Also 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 <T> List<T> list(String alias, Class<T> type)
SqlQuery
SqlQuery.object(String, Class)
for scalar value
definition. This method uses internally Query.list()
.
List<Integer> prices = sm.SQL("SELECT price FROM stock").list("price", Integer.class);
public <T> List<T> list(Class<T> entity)
SqlQuery
SqlQuery.object(Class)
for a discussion about entities.
This method uses internally Query.list()
. Usage pattern is like below:
List<Person> persons = sm.SQL("SELECT id,name FROM person").list(Person.class);
public <K,V> Map<K,V> map()
SqlQuery
Map<String, Integer> serialMap = sm.SQL("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 column position in query, first is key and the second is value.
public void update()
SqlQuery
public void delete()
SqlQuery
SqlQuery.update()
.private void scalar(String alias, Class<?> type)
types
for supported Java types.alias
- column name or alias,type
- Java type.IllegalArgumentException
- if requested Java type is not supported.private org.hibernate.SQLQuery query(Class<?>... entity)
entity
- optional Hibernate entity.Copyright © 2019. All rights reserved.