gpt4 book ai didi

spring-data - 嵌套 DTO 中的查询结果

转载 作者:行者123 更新时间:2023-12-05 07:34:58 26 4
gpt4 key购买 nike

Spring Data 中是否不允许 JPQL 查询中的嵌套 DTO? :

@Query("SELECT new test.customresult.CategoryCounter(c.name, " 
+ "new test.customresult.CarResult(COUNT(e.category), e.category)"
+ "FROM error e JOIN e.car c "
+ "GROUP BY c.name,e.category")
List<CategoryCounter>countErrors();

因为,我在尝试使用上述 JPQL 查询时收到以下错误消息:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , near line 1, 

我想从 JPQL 查询返回这样一个 JSON 文件:

[{
"Car 1":
[{
"count": 1,
"category": "A"
}, {
"count": 2,
"category": "B"
}, {
"count": 0,
"category": "C"
}, {
"count": 0,
"category": "D"
}
]

}, {
"Car 2":
[{
"count": 0,
"category": "A"
}, {
"count": 0,
"category": "B"
}, {
"count": 4,
"category": "C"
}, {
"count": 5,
"category": "D"
}
]

}
]

有一个car 表和一个error 表,其中包含类别和car 表的外键。

我想使用嵌套的 DTO 来表示所需的输出:

“包装器”DTO

public class CategoryCounter {

private String name;
private CarResult carResult ;

public CategoryCounter (String name, CarResult carResult ) {

this.name= name;
this.carResult = carResult ;
}


public CarResult getCarResult () {
return carResult ;
}

public void setCarResult(CarResult carResult ) {
this.carResult = carResult ;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name= name;
}

}

和“嵌套”DTO:

public class CarResult {


private Long count;
private String category;

public CarResult (Long count, String category) {

this.count = count;
this.category= category;
}


public Long getCount() {
return count;
}

public void setCount(Long count) {
this.count = count;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category= category;
}
}

最佳答案

我不确定这是否是问题所在...但是您的 FROM 子句紧接在 ) 之后开始,它说:

+ "new test.customresult.CarResult(COUNT(e.category), e.category)"
+ "FROM error e JOIN e.car c "

这与:e.category)FROM 相同,因此在该部分放置一个空格并重试...

+ "new test.customresult.CarResult(COUNT(e.category), e.category)"
+ " FROM error e JOIN e.car c "

问题是错误(由 org.hibernate.hql.internal.ast.QuerySyntaxException 引起)是一个语法错误

关于spring-data - 嵌套 DTO 中的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49867670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com