我目前正在编写一个报告,其中一个字段需要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);
Run Code Online (Sandbox Code Playgroud)
这会在行上抛出一个空指针异常:
TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);
Run Code Online (Sandbox Code Playgroud)
我错误地使用了criteriaBuilder的函数方法吗?
文件说:
function(String name, Class<T> type, Expression<?>... args);
Run Code Online (Sandbox Code Playgroud)
我想通过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) + ")";
}
Run Code Online (Sandbox Code Playgroud)
}
2.)我创建延伸org.hibernate.dialect.MySQL5Dialect的CustomMySql5Dialect类和登记在步骤1中创建的GROUP_CONCAT类
3)在该应用的上下文,我更新的jpaVendorAdapter使用CustomMySql5Dialect作为databasePlatform
4)最后使用它
criteriaBuilder.function("group_concat", String.class,
sampleRoot.get("sampleColumnName"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6138 次 |
| 最近记录: |