我在当前的 spring-boot 项目中使用 JPA 查询。如何添加非标准化 SQL 函数,如GROUP_CONCAT?
之前,我以前的问题: 如何在 JPA 查询中的一行逗号分隔列表中显示列结果
我发现 GROUP_CONCAT 不是 JPA 查询中的注册函数,但可以通过手动注册来访问它。我已经尝试过以下链接,但对我不起作用:
如何在 Spring Boot 应用程序中添加非标准化的 sql 函数?
https://thoughts-on-java.org/database-functions/
https://vladmihalcea.com/hibernate-sql-function-jpql-criteria-api-query/
1.
public class SqlFunctionsMetadataBuilderContributor
implements MetadataBuilderContributor {
@Override
public void contribute(MetadataBuilder metadataBuilder) {
metadataBuilder.applySqlFunction(
"group_concat",
new StandardSQLFunction(
"group_concat",
StandardBasicTypes.STRING
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
2.
public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory)
throws QueryException {
if (arguments.size() < 1) {
throw new QueryException(new IllegalArgumentException("group_concat should have at …Run Code Online (Sandbox Code Playgroud)