gpt4 book ai didi

java - Spring 数据 - JPA : issue query with DTO and IF statement

转载 作者:行者123 更新时间:2023-11-29 06:54:43 25 4
gpt4 key购买 nike

我正在尝试进行一个构建 DTO 对象的查询:

@Query("Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, IF(a.salarie,'yes','no'))   
from Activity a where a.year = ?1 and a.month = ?2")
List<AnalyseProductionDTO> getAnalyseProduction(Year year, Month month);

当我在查询中放入 IF 语句时,它不起作用。
我得到了一个

java.lang.IllegalArgumentException: Validation failed for query

当我这样做时它就起作用了:

@Query("Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, 'yes')   
from Activity a where a.year = ?1 and a.month = ?2")
List<AnalyseProductionDTO> getAnalyseProduction(Year year, Month month);

是否可以在查询中使用的构造函数中包含 IF 语句?

最佳答案

默认情况下@Query不允许 native 查询。如果 ORM 处于 hibernate 状态,则查询应该是 HQL,或者是底层 ORM 支持的任何内容。

由于 IF() 函数是 MySQL 原生的且不受 ORM 查询语言支持,因此其验证将失败。

您可以将 @Query.nativeQuery 设置为 true 来尝试以下操作。

@Query(value = "Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, IF(a.salarie,'yes','no'))   
from Activity a where a.year = ?1 and a.month = ?2", nativeQuery = true)

关于java - Spring 数据 - JPA : issue query with DTO and IF statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46077054/

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