是否可以使用JPA具有自定义的聚合函数,该函数将扩展concat()以便将列值连接为单个字符串?
最佳答案
我最近在使用JPA和H2数据库时遇到了同样的问题。我尝试从JPQL调用带有函数调用(参见下文)的GROUP_CONCAT函数,但未成功。最后,我改用了 native 查询调用。
但是,JPA 2.1 specifications提到您可以调用(函数调用)标量或聚集的预定义或用户定义的数据库函数。此后,我通过高亮显示相关部分,从规范中复制了该段。
4.6.17.3 Invocation of Predefined and User-defined Database Functions
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
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.
Applications that use the function_invocation syntax will not be portable across databases.
Example:
SELECT c FROM Customer c WHERE FUNCTION(‘hasGoodCredit’, c.balance, c.creditLimit)
引用:Java Persistence 2.1, Final Release
关于JPA将STRING列值连接为单个值,就像聚合函数一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23521118/