Package net.sf.basedb.core.query
Class Hql
java.lang.Object
net.sf.basedb.core.query.Hql
A factory class to create expressions, joins, etc. that are only used
by HQL queries. The elements will throw an
UnsupportedOperationException
if passed to
a SQL query.- Version:
- 2.0
- Author:
- Nicklas
- Last modified
- $Date: 2016-03-10 09:50:45 +0100 (to, 10 mar 2016) $
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Pattern
An alias can only contain the characters a-z, A-Z or 0-9.private static final Logger
Log core events.static final Pattern
A property can only contain the characters a-zA-Z0-9, period(.), underscore(_) or hash (#) It cannot begin or end with a period/hash and cannot have two periods/hashes in a sequence. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Expression
Same asproperty(alias, null)
.static Expression
Create an elements expression for a property that is a collection of values.static Expression
Create an expression representing an item which is a subclass of BasicItem.static Expression
Create an expression representing an item which is a subclass of BasicData, for example a reporter.static Expression
entityParameter
(String name, Item itemType) Create an expression for a parameter which must be an entity of a specified type.static Expression
expression
(String exprString, String prefix) static Expression
Create an index expression for a property that is a map or a list.static Join
Same asinnerJoin(null, propert, joinedAlias)
static Join
Create an inner join query element.static Join
Same asinnerJoin(null, propert, joinedAlias, null, false)
static Join
innerJoin
(String alias, String property, String joinedAlias, Restriction on, boolean fetch) Create an inner join query element and optionally prefetch the joined items in the same query.static Restriction
isNotPartOf
(String alias, String property, ReporterList reporterList) static Restriction
isPartOf
(String alias, String property, ReporterList reporterList) static Join
Same asleftJoin(null, propert, joinedAlias, null, false)
static Join
leftJoin
(String alias, String property, String joinedAlias, Restriction on, boolean fetch) Create a left join query element.static Expression
Same asproperty(null, property)
.static Expression
Create an expression representing a property of an object or joined alias.static Expression
property
(String alias, String property, Expression index) Create an expression representing the value of an indexed property.static Restriction
restriction
(String restrictionString, String prefix) static Join
Same asrightJoin(null, propert, joinedAlias, null)
static Join
rightJoin
(String alias, String property, String joinedAlias, Restriction on) Create a right join query element.static Restriction
sharedTo
(boolean sharedTo, Restriction users, Restriction groups, Restriction projects) A special restriction that works onShareable
items.static Expression
Create a size expression for a property that is a collection of values.
-
Field Details
-
log
Log core events. -
PROPERTY_REGEXP
A property can only contain the characters a-zA-Z0-9, period(.), underscore(_) or hash (#) It cannot begin or end with a period/hash and cannot have two periods/hashes in a sequence. A hash indicates a path to a component while a period indicates a path to another entity. -
ALIAS_REGEXP
An alias can only contain the characters a-z, A-Z or 0-9.
-
-
Constructor Details
-
Hql
public Hql()
-
-
Method Details
-
entity
Create an expression representing an item which is a subclass of BasicItem. In the query the actual value used is the id of the entity. Seeproperty(String, String)
for a code example.- Parameters:
entity
- The entity- Returns:
- an object implementing
Expression
- Throws:
InvalidDataException
- If the entity is null or isn't saved to the database- See Also:
-
entity
Create an expression representing an item which is a subclass of BasicData, for example a reporter. In the query the actual value used is the id of the entity. Seeproperty(String, String)
for a code example.- Parameters:
entity
- The entity- Throws:
InvalidDataException
- If the entity is null- Since:
- 2.2.2
- See Also:
-
entityParameter
Create an expression for a parameter which must be an entity of a specified type.- Parameters:
name
- The name of the parameter. Can not be nullitemType
- The type of the- Returns:
- An expression representing the parameter
- Throws:
InvalidDataException
- If the 'name' is null or contains invalid characters or if 'itemType' is null- Since:
- 2.17
-
property
Same asproperty(null, property)
.- Throws:
InvalidDataException
- See Also:
-
alias
Same asproperty(alias, null)
.- Throws:
InvalidDataException
- See Also:
-
property
Create an expression representing a property of an object or joined alias. Property names are usually named after a getter method minus the "get" part. Ie. agetName()
method corresponds to a "name" property. If no alias is given the property name is resolved against the root entity of the query, otherwise it is resolved against the joined entity with the given alias.// Sorting user by the "name" property ItemQuery<User> query = User.getQuery(); query.order(Orders.asc(Hql.property("name"))); // Generated HQL SELECT usr FROM UserData usr ORDER BY usr.name ASC // Loading users members of the Administrators role DbControl dc = ... int adminId = SystemItems.get(Role.ADMINISTRATOR); // Assume adminId = 1 Role admin = Role.getById(dc, adminId); ItemQuery<User> query = User.getQuery(); query.join(Hql.innerJoin("roles", "rle")); query.restrict( Restrictions.eq( Hql.alias("rle"), Hql.entity(admin) ) ); // Generated HQL SELECT usr FROM UserData usr JOIN usr.roles rle WHERE rle = 1
- Parameters:
property
- The property name of the objectalias
- Alias to resolve the property against, use null to resolve the property agains the root entity- Returns:
- An expression representing the property
- Throws:
InvalidDataException
- If both the property and alias is null or any of the parameters contains invalid characters
-
property
Create an expression representing the value of an indexed property. An indexed property is an element in a list, array or map. The alias and property is used as inproperty(String, String)
. The index expression is applied as:property[index]
.- Parameters:
property
- The property name of the objectalias
- Alias to resolve the property against, use null to resolve the property agains the root entityindex
- An index expression (null is allowed)- Returns:
- An expression representing the indexed property
- Since:
- 3.8
-
index
Create an index expression for a property that is a map or a list. If no alias is given the property name is resolved against the root entity of the query, otherwise it is resolved against the joined entity with the given alias. If no property is given only the alias is used.- Parameters:
alias
- The alias to resolve the property agains, or null to resolve the property against the root entityproperty
- The property name of the object which must be a map or list association- Returns:
- An expression representing the index collection of the property
- Throws:
InvalidDataException
- If both the alias and property parameters are null or if any of them contains invalid characters
-
elements
Create an elements expression for a property that is a collection of values. If no alias is given the property name is resolved against the root entity of the query, otherwise it is resolved against the joined entity with the given alias.- Parameters:
property
- The property name of the object which must set, map or other collection- Returns:
- An expression representing the values collection of the property
- Throws:
InvalidDataException
- If both the alias and property parameters are null or if any of them contains invalid characters
-
size
Create a size expression for a property that is a collection of values. If no alias is given the property name is resolved against the root entity of the query, otherwise it is resolved against the joined entity with the given alias.- Parameters:
property
- The property name of the object which must set, map or other collection- Returns:
- An expression representing the size of the collection
- Throws:
InvalidDataException
- If both the alias and property parameters are null or if any of them contains invalid characters- Since:
- 2.7
-
innerJoin
Same asinnerJoin(null, propert, joinedAlias)
- Throws:
InvalidDataException
- See Also:
-
innerJoin
public static Join innerJoin(String alias, String property, String joinedAlias) throws InvalidDataException Create an inner join query element. An inner join only selects an item if it has an associated item. The property must reference an association to another item or collection of values. Seeproperty(String, String)
for a code example.- Parameters:
alias
- The alias to resolve the property against, or null to resolve the property against the root entityproperty
- The property name of the associated entity (required)joinedAlias
- The alias to give the joined entity (required)- Returns:
- A join query element
- Throws:
InvalidDataException
- If the property or joined alias parameter are null or if any of the parameters contains invalid characters
-
innerJoin
public static Join innerJoin(String alias, String property, String joinedAlias, boolean fetch) throws InvalidDataException Same asinnerJoin(null, propert, joinedAlias, null, false)
- Throws:
InvalidDataException
- See Also:
-
innerJoin
public static Join innerJoin(String alias, String property, String joinedAlias, Restriction on, boolean fetch) throws InvalidDataException Create an inner join query element and optionally prefetch the joined items in the same query. This is useful to avoid subsequent queries to the database when you know that you are going to access the joined items in your code. If you are joining a collection theQuery.setFirstResult(int)
andQuery.setMaxResults(int)
shouldn't be used since the join will create multiple rows for the same root item.- Parameters:
alias
- The alias to resolve the property against, or null to resolve the property against the root entityproperty
- The property name of the associated entity (required)joinedAlias
- The alias to give the joined entity (required)on
- Optional extra restriction that is used in theON
clause of the generated queryfetch
- If the joined items should be prefetched or not- Returns:
- A join query element
- Throws:
InvalidDataException
- If the property or joined alias parameter are null or if any of the parameters contains invalid characters- Since:
- 2.8
-
leftJoin
Same asleftJoin(null, propert, joinedAlias, null, false)
- Throws:
InvalidDataException
- See Also:
-
leftJoin
public static Join leftJoin(String alias, String property, String joinedAlias, Restriction on, boolean fetch) throws InvalidDataException Create a left join query element. A left join selects all items from the root entity even if they don't have an associated item. The property must reference an association to another item or collection of values.// Find all users not assigned to a role ItemQuery<User> query = User.getQuery(); query.join(Hql.leftJoin("roles", "rle")); query.restrict( Restrictions.eq( Hql.alias("rle"), null ) ); // Generated HQL SELECT usr FROM UserData usr LEFT JOIN usr.roles rle WHERE rle IS NULL
The
fetch
parameter is used to fetch the joined data in the same query. This is useful to avoid subsequent queries to the database when you know that you are going to access the joined items in your code. If you are joining a collection theQuery.setFirstResult(int)
andQuery.setMaxResults(int)
shouldn't be used since the join will create multiple rows for the same root item.- Parameters:
alias
- The alias to resolve the property against, or null to resolve the property against the root entityproperty
- The property name of the associated entity (required)joinedAlias
- The alias to give the joined entity (required)on
- Optional extra restriction that is used in theON
clause of the generated queryfetch
- If the joined items should be prefetched or not- Returns:
- A join query element
- Throws:
InvalidDataException
- If the property or joined alias parameter are null or if any of the parameters contains invalid characters
-
rightJoin
Same asrightJoin(null, propert, joinedAlias, null)
- Throws:
InvalidDataException
- See Also:
-
rightJoin
public static Join rightJoin(String alias, String property, String joinedAlias, Restriction on) throws InvalidDataException Create a right join query element. A right join selects all items from the joined association even if they don't have an associated item in the root entity. The property must reference an association to another item or collection of values.- Parameters:
alias
- The alias to resolve the property against, or null to resolve the property against the root entityproperty
- The property name of the associated entity (required)joinedAlias
- The alias to give the joined entity (required)on
- Optional extra restriction that is used in theON
clause of the generated query- Returns:
- A join query element
- Throws:
InvalidDataException
- If the property or joined alias parameter are null or if any of the parameters contains invalid characters
-
restriction
- Parameters:
restrictionString
- A restriction as a string. Null or empty string is not allowed.prefix
- Prefix used in the restriction string. The prefix will be replaced in the returned object by the query's root-alias. Null is allowed.- Returns:
- a Restriction to use in a query.
- Since:
- 2.5
-
expression
- Parameters:
exprString
- An expression as a string. Null or empty string is not allowed.prefix
- Prefix used in the expression string. The prefix will be replaced in the returned object by the query's root-alias. Null is allowed.- Returns:
- an Object implementing
Expression
. - Since:
- 2.5
-
isPartOf
-
isNotPartOf
-