JPA Criteria API group_concat 用法

发布于 2024-11-29 07:13:26 字数 926 浏览 1 评论 0原文

我目前正在编写一份报告,其中一个字段需要 group_concat 。

CriteriaQuery<GameDetailsDto> criteriaQuery = criteriaBuilder
                .createQuery(GameDetailsDto.class);
Root<BetDetails> betDetails = criteriaQuery.from(BetDetails.class);
Expression<String> betSelection = betDetails.get("winningOutcome");
criteriaQuery.multiselect(
    // other fields to select
    criteriaBuilder.function("group_concat", String.class, betSelection),
    // other fields to select
    );
//predicate, where clause and other filters

TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);

这会引发空指针异常:

TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);

我是否错误地使用了 criteriaBuilder 的函数方法?
文件说:

function(String name, Class<T> type, Expression<?>... args);

I am currently working on a report which needs a group_concat for one of the fields.

CriteriaQuery<GameDetailsDto> criteriaQuery = criteriaBuilder
                .createQuery(GameDetailsDto.class);
Root<BetDetails> betDetails = criteriaQuery.from(BetDetails.class);
Expression<String> betSelection = betDetails.get("winningOutcome");
criteriaQuery.multiselect(
    // other fields to select
    criteriaBuilder.function("group_concat", String.class, betSelection),
    // other fields to select
    );
//predicate, where clause and other filters

TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);

this throws a null pointer exception on the line:

TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);

did i incorrectly use the function method of the criteriaBuilder?

the documentations says:

function(String name, Class<T> type, Expression<?>... args);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花似海 2024-12-06 07:13:26

我想出了如何使用 Hibernate-jpa-mysql 执行此操作:

1.) 创建了一个扩展 org.hibernate.dialect.function.SQLFunction 的 GroupConcatFunction 类(目前用于单列 group_concat)

public class GroupConcatFunction implements SQLFunction {

@Override
public boolean hasArguments() {
    return true;
}

@Override
public boolean hasParenthesesIfNoArguments() {
    return true;
}

@Override
public Type getReturnType(Type firstArgumentType, Mapping mapping)
        throws QueryException {
    return StandardBasicTypes.STRING;
}

@Override
public String render(Type firstArgumentType, List arguments,
        SessionFactoryImplementor factory) throws QueryException {
    if (arguments.size() != 1) {
        throw new QueryException(new IllegalArgumentException(
                "group_concat shoudl have one arg"));
    }
    return "group_concat(" + arguments.get(0) + ")";
}

}

2.) 我创建了扩展 org.hibernate.dialect.MySQL5Dialect 的 CustomMySql5Dialect 类,并注册了步骤 1 中创建的 group_concat 类

3.)在应用程序上下文中,我更新了 jpaVendorAdapter 以使用 CustomMySql5Dialect 作为数据库平台

4.)终于可以使用它了

criteriaBuilder.function("group_concat", String.class,
        sampleRoot.get("sampleColumnName"))

I figured out how to do this with Hibernate-jpa-mysql:

1.) created a GroupConcatFunction class extending org.hibernate.dialect.function.SQLFunction (this is for single column group_concat for now)

public class GroupConcatFunction implements SQLFunction {

@Override
public boolean hasArguments() {
    return true;
}

@Override
public boolean hasParenthesesIfNoArguments() {
    return true;
}

@Override
public Type getReturnType(Type firstArgumentType, Mapping mapping)
        throws QueryException {
    return StandardBasicTypes.STRING;
}

@Override
public String render(Type firstArgumentType, List arguments,
        SessionFactoryImplementor factory) throws QueryException {
    if (arguments.size() != 1) {
        throw new QueryException(new IllegalArgumentException(
                "group_concat shoudl have one arg"));
    }
    return "group_concat(" + arguments.get(0) + ")";
}

}

2.) i created the CustomMySql5Dialect class extending org.hibernate.dialect.MySQL5Dialect and registered the group_concat class created in step 1

3.) On the app context, i updated the jpaVendorAdapter to use the CustomMySql5Dialect as the databasePlatform

4.) Finally to use it

criteriaBuilder.function("group_concat", String.class,
        sampleRoot.get("sampleColumnName"))
南街九尾狐 2024-12-06 07:13:26

简单的解决方案:无需创建整个类,只需使用 SQLFunctionTemplate 即可。

new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)")

然后用你自己的 SQL 方言注册这个函数(例如在构造函数中)

public class MyOwnSQLDialect extends MySQL5Dialect {

  public MyOwnSQLDialect() {
    super();
    this.registerFunction("group_concat", new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)"));
  }
}

Simple solution: instead of creating the whole class, just use SQLFunctionTemplate.

new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)")

and then register this function in your own SQL dialect (eg. in constructor)

public class MyOwnSQLDialect extends MySQL5Dialect {

  public MyOwnSQLDialect() {
    super();
    this.registerFunction("group_concat", new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)"));
  }
}
吹泡泡o 2024-12-06 07:13:26

建议属性:

spring.jpa.properties.hibernate.metadata_builder_contributor = com.inn.core.generic.utils.SqlFunctionsMetadataBuilderContributor

和类别:

import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.spi.MetadataBuilderContributor;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Component;
@Component
public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor {

    @Override
    public void contribute(MetadataBuilder metadataBuilder) {
        metadataBuilder.applySqlFunction("config_json_extract",
                new StandardSQLFunction("json_extract", StandardBasicTypes.STRING));
        metadataBuilder.applySqlFunction("JSON_UNQUOTE",
                new StandardSQLFunction("JSON_UNQUOTE", StandardBasicTypes.STRING));
        metadataBuilder.applySqlFunction("group_concat", 
                new StandardSQLFunction("group_concat", StandardBasicTypes.STRING));

    }

}

Suggested property:

spring.jpa.properties.hibernate.metadata_builder_contributor = com.inn.core.generic.utils.SqlFunctionsMetadataBuilderContributor

and class:

import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.spi.MetadataBuilderContributor;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Component;
@Component
public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor {

    @Override
    public void contribute(MetadataBuilder metadataBuilder) {
        metadataBuilder.applySqlFunction("config_json_extract",
                new StandardSQLFunction("json_extract", StandardBasicTypes.STRING));
        metadataBuilder.applySqlFunction("JSON_UNQUOTE",
                new StandardSQLFunction("JSON_UNQUOTE", StandardBasicTypes.STRING));
        metadataBuilder.applySqlFunction("group_concat", 
                new StandardSQLFunction("group_concat", StandardBasicTypes.STRING));

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文