JPA concat operator
Solution 1
The CONCAT
function was extended in JPA 2.0 to allow passing more than 2 parameters, from section 4.6.17.2.1 (String Functions) of the specification:
CONCAT(string_primary, string_primary {, string_primary}* )
In JPA 1 this was restricted to exactly two parameters.
Solution 2
You can use JPA Concat function for multiple strings.
For example:
CONCAT(cola, colb, colc, cold)
Solution 3
You can also use ||
as a concatenate operator, see on the documentation
HQL defines a concatenation operator in addition to supporting the concatenation (CONCAT) function. This is not defined by JPQL, so portable applications should avoid it use. The concatenation operator is taken from the SQL concatenation operator - ||.
Example 11.19. Concatenation operation example
select 'Mr. ' || c.name.first || ' ' || c.name.last from Customer c where c.gender = Gender.MALE
Udo Held
Updated on October 20, 2020Comments
-
Udo Held over 3 years
Is there a JPA concat operator for string concatenation?
I know there is a JPA
CONCAT
function, however its ugly to use for concatenating multiple strings.CONCAT(CONCAT(CONCAT(cola,colb),colc),cold)
Vendors like Oracle offer
||
some other like Microsoft offer+
. Is there a standard JPA concatenation operator so that I could create a query likecola || colb || colc || cold
I tried
+
using openjpa with SQL Server, however it seems to be invalid JPQL. I couldn't find anything regarding such an operator in an oracle reference. -
Udo Held over 12 yearsChecked that. I get an exception with openjpa. The reference I linked (docs.oracle.com/cd/E16764_01/apirefs.1111/e13946/…) shows only two arguments as well.
-
Udo Held over 12 yearsHmm. Strange. download.oracle.com/otn-pub/jcp/… lists this function as you described it. Openjpa 2.1.1 should be JPA 2.0.
-
Jörn Horstmann over 12 yearsOpenJpa 2 should support this syntax, can you post the exact query and the exception you are receiving?