Movatterモバイル変換


[0]ホーム

URL:


ObjectDBObjectDB
ObjectDB Manual

Strings in JPQL and Criteria Queries

String values may appear in JPQL queries in various forms:

This page covers the following topics:

LIKE - String Pattern Matching with WildcardsLENGTH - Counting Characters in a StringLOCATE - Locating SubstringsLOWER and UPPER - Changing String CaseTRIM - Stripping Leading and Trailing CharactersCONCAT - String ConcatenationSUBSTRING - Getting a Portion of a StringJava String Methods (ObjectDB Extension)Criteria Query String Expressions

LIKE - String Pattern Matching with Wildcards

The [NOT] LIKE operator checks if a specified string matches a specified pattern. The pattern  may include ordinary characters as well as the following wildcard characters:

The left operand is always the string to check for a match (usually a path expression) and the right operand is always the pattern (usually a parameter or literal). For example:

To match an actual underscore or percent character it has to be preceded by an escape character, which is also specified. For example:

In the expressions above only the first percent character (%) is a wildcard. The second (which appears after the escape character) represents a real% character.

LENGTH - Counting Characters in a String

TheLENGTH(str) function returns the number of characters in the argument string as anint.

For example:

LOCATE - Locating Substrings

TheLOCATE(str, substr [, start]) function searches a substring and returns its position.

For example:

Notice that positions are one-based (as in SQL) rather than zero-based (as in Java). Therefore, the position of the first character is1. Zero (0) is returned if the substring is not found.

The third argument (when present) specifies from which position to start the search.

LOWER and UPPER - Changing String Case

The LOWER(str) and UPPER(str) functions return a string after conversion to lowercase or uppercase (respectively).

For example:

TRIM - Stripping Leading and Trailing Characters

TheTRIM([[LEADING|TRAILING|BOTH] [char] FROM] str) function returns a string after removing leading and/or trailing characters (usually space characters).

For example:

By default, space characters are removed, but any other character can also be specified:

CONCAT - String Concatenation

TheCONCAT(str1, str2, ...) function returns the concatenation of the specified strings.

For example:

SUBSTRING - Getting a Portion of a String

TheSUBSTRING(str, pos [, length]) function returns a substring of a specified string.

For example:

Notice that positions are one-based (as in SQL) rather than zero-based (as in Java). If length is not specified (the third optional argument), the entire string suffix, starting at the specified position, is returned.

Java String Methods (ObjectDB Extension)

ObjectDB also supports ordinary Java String methods.

For example:

Thematches method of theString class can be useful when there is a need for pattern matching using regular expressions (which are more powerful than theLIKE operator).

Criteria Query String Expressions

JPQL string operators and functions (which are described above) are available also as JPA criteria query expressions. TheCriteriaBuilderjakarta.persistence.criteria.CriteriaBuilder - JPA Interface Used to construct criteria queries, compound selections, expressions, predicates, orderings. interface provides factory methods for building these expressions, as shown in the following examples:

// Create path and parameter expressions:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> path= country.getPath.get(attributeName)-JPAMethodCreate a path correspondingto the referenced attribute.("name");Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> param= cb.parameterCriteriaBuilder.parameter(paramClass)-JPAMethodCreate a parameter expression.(String.class);// str [NOT] LIKE patternPredicatejakarta.persistence.criteria.Predicate-JPAInterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. l1= cb.likeCriteriaBuilder.like(x,pattern)-JPAMethodCreate a predicatefor testing whether the expression satisfies the given pattern.(path, param);Predicatejakarta.persistence.criteria.Predicate-JPAInterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. l2= cb.likeCriteriaBuilder.like(x,pattern)-JPAMethodCreate a predicatefor testing whether the expression satisfies the given pattern.(path,"a%");Predicatejakarta.persistence.criteria.Predicate-JPAInterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. l3= cb.notLikeCriteriaBuilder.notLike(x,pattern)-JPAMethodCreate a predicatefor testing whether the expression does not satisfy the given pattern.(path, param);Predicatejakarta.persistence.criteria.Predicate-JPAInterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. l4= cb.notLikeCriteriaBuilder.notLike(x,pattern)-JPAMethodCreate a predicatefor testing whether the expression does not satisfy the given pattern.(path,"a%");// additional methods take also an escape character// LENGTH(str)Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> length= cb.lengthCriteriaBuilder.length(x)-JPAMethodCreate expressiontoreturn length of a string.(path);// LOCATE(str, substr [, start])Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> l1= cb.locateCriteriaBuilder.locate(x,pattern)-JPAMethodCreate expressionto locate the position of one string within another, returning position of first characterif found.(path, param);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> l2= cb.locateCriteriaBuilder.locate(x,pattern)-JPAMethodCreate expressionto locate the position of one string within another, returning position of first characterif found.(path,"x");Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> l3= cb.locateCriteriaBuilder.locate(x,pattern,from)-JPAMethodCreate expressionto locate the position of one string within another, returning position of first characterif found.(path, param, cb.literalCriteriaBuilder.literal(value)-JPAMethodCreate an expressionfor a literal.(2));Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> l4= cb.locateCriteriaBuilder.locate(x,pattern,from)-JPAMethodCreate expressionto locate the position of one string within another, returning position of first characterif found.(path,"x",2);// LOWER(str) and UPPER(str)Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> lower= cb.lowerCriteriaBuilder.lower(x)-JPAMethodCreate expressionfor converting a stringto lowercase.(path);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> upper= cb.upperCriteriaBuilder.upper(x)-JPAMethodCreate expressionfor converting a stringto uppercase.(param);// TRIM([[LEADING|TRAILING|BOTH] [char] FROM] str)Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> t1= cb.trimCriteriaBuilder.trim(x)-JPAMethodCreate expressionto trim blanks from both ends of a string.(path);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> t2= cb.trimCriteriaBuilder.trim(t,x)-JPAMethodCreate expressionto trim character from both ends of a string.(literal(' '), path);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> t3= cb.trimCriteriaBuilder.trim(t,x)-JPAMethodCreate expressionto trim character from both ends of a string.(' ', path);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> t4= cb.trimCriteriaBuilder.trim(ts,x)-JPAMethodCreate expressionto trim blanks from a string.(Trimspec.BOTH, path);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> t5= cb.trimCriteriaBuilder.trim(ts,t,x)-JPAMethodCreate expressionto trim character from a string.(Trimspec.LEADING,literalCriteriaBuilder.literal(value)-JPAMethodCreate an expressionfor a literal.(' '), path);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> t6= cb.trimCriteriaBuilder.trim(ts,t,x)-JPAMethodCreate expressionto trim character from a string.(Trimspec.TRAILING, ' ', path);// CONCAT(str1, str2)Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> c1= cb.concatCriteriaBuilder.concat(x,y)-JPAMethodCreate an expressionfor string concatenation.(path, param);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> c2= cb.concatCriteriaBuilder.concat(x,y)-JPAMethodCreate an expressionfor string concatenation.(path,".");Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> c3= cb.concatCriteriaBuilder.concat(x,y)-JPAMethodCreate an expressionfor string concatenation.("the", path);// SUBSTRING(str, pos [, length])Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> s1= cb.substringCriteriaBuilder.substring(x,from)-JPAMethodCreate an expressionfor substring extraction.(path, cb.literalCriteriaBuilder.literal(value)-JPAMethodCreate an expressionfor a literal.(2));Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> s2= cb.substringCriteriaBuilder.substring(x,from)-JPAMethodCreate an expressionfor substring extraction.(path,2);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> s3=    cb.substringCriteriaBuilder.substring(x,from,len)-JPAMethodCreate an expressionfor substring extraction.(path, cb.literalCriteriaBuilder.literal(value)-JPAMethodCreate an expressionfor a literal.(2), cb.literalCriteriaBuilder.literal(value)-JPAMethodCreate an expressionfor a literal.(3));Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<String> s4= cb.substringCriteriaBuilder.substring(x,from,len)-JPAMethodCreate an expressionfor substring extraction.(path,2,3);

As demonstrated above, most methods are overloaded in order to support optional arguments and when applicable simple Java objects as well as criteria expressions.

< Numbers in JPQL^ Query ExpressionsDate and Time in JPQL >

[8]ページ先頭

©2009-2025 Movatter.jp