【问题标题】:Query results in nested DTO嵌套 DTO 中的查询结果
【发布时间】:2018-04-16 23:33:11
【问题描述】:

在 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;
    }
}

【问题讨论】:

    标签: spring-data


    【解决方案1】:

    我不确定这是否是问题所在...但是您的 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 引起)是语法错误

    【讨论】:

    • 感谢您的评论。我在您建议的字段中放置了一个空格。但不幸的是,仍然显示相同的错误消息,并且它指向 c.name, new 中的逗号之后
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多