JPA将STRING列值连接为单个值,就像聚合函数一样 [英] JPA concatenate STRING column values into a single value just like an aggregate function
问题描述
是否可以使用JPA具有自定义的聚合函数,该函数将扩展concat()以便将列值连接为单个字符串?
Is it possible using JPA to have a custom aggregation function that would extend concat() so that it concatenates a column values into a single string?
推荐答案
我最近在使用JPA和H2数据库时遇到了同样的问题.我试图通过函数调用从JPQL调用 GROUP_CONCAT 函数(请参见下文)没有成功.最后,我改用了本地查询调用.
I faced the same problem recently with JPA and H2 database. I tried to call the GROUP_CONCAT function from JPQL with a function invocation (see below) with no success. Finally, I used a native query call instead.
但是, JPA 2.1规范提到您可以调用(函数调用)一个标量或聚集的预定义或用户定义的数据库函数.此后,我通过 突出显示 相关部分从规范中复制了该段.
However, the JPA 2.1 specifications mention that you can call (function invocation) a predefined or user-defined database function either scalar or aggregate one. I have reproduced thereafter the paragraph from the specfication by highlighting the relevant part.
4.6.17.3调用预定义和用户定义的数据库功能
通过function_invocation
语法支持Java Persistence查询语言的内置功能以外的功能的调用.这包括调用预定义的数据库功能和用户定义的数据库功能.
The invocation of functions other than the built-in functions of the Java Persistence query language is supported by means of the function_invocation
syntax. This includes the invocation of predefined database functions and user-defined database functions.
function_invocation::= FUNCTION(function_name {, function_arg}*)
function_arg ::=
literal |
state_valued_path_expression |
input_parameter |
scalar_expression
function_name参数是一个字符串,它表示要调用的数据库函数.参数必须适合于要调用的数据库功能.函数的结果必须适合于调用上下文.
The function_name argument is a string that denotes the database function that is to be invoked. The arguments must be suitable for the database function that is to be invoked. The result of the function must be suitable for the invocation context.
该功能可以是数据库定义的功能,也可以是用户定义的功能. 函数可以是标量函数或聚合函数 .
The function may be a database-defined function or a user-defined function. The function may be a scalar function or an aggregate function.
使用function_invocation语法的应用程序将无法在数据库之间移植.
Applications that use the function_invocation syntax will not be portable across databases.
示例:
SELECT c
FROM Customer c
WHERE FUNCTION(‘hasGoodCredit’, c.balance, c.creditLimit)
参考: Java Persistence 2.1,最终发行版
这篇关于JPA将STRING列值连接为单个值,就像聚合函数一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!