JPA / Hibernate + HQL / JPQL:使用BigDecimal参数选择DTO

问题描述 投票:2回答:4

我们正在将JPA与休眠一起用作实现。假设我有以下DTO:

public class SupplierInfoDto{
   private String supplierName;
   private BigDecimal remainingFinances;

   public SupplierInfoDto(String supplierName, BigDecimal remainingFinances){
       this.supplierName = supplierName;
       this.remainingFinances = remainingFinances;
   }

   // getters / setters
}

我似乎无法休眠以正确找到此构造函数。我首先尝试了以下查询(该模型比这更复杂,并且最终需要获取一些聚合(而不是直接在实体上),这就是为什么我要获取DTO而不是实体):

SELECT NEW com.company.dto.SupplierInfoDto(s.name, f.remaining)
FROM Supplier s INNER JOIN Finances f
WHERE s.id = :SupplierId

但是,我得到了org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class例外。

[我要从中选择的remaining列存储为MSSQL中的浮点数(我知道,我知道钱永远不应存储为浮点数,但这是一个现有的系统,我不能只更改此数据类型)。。] >

作为测试,我尝试了以下查询,但与上述相同:

SELECT NEW com.company.dto.SupplierInfoDto(s.name, NEW java.math.BigDecimal(10))
FROM Supplier s
WHERE s.id = :SupplierId

所以我的问题是:如何使hibernate / JPA为上面的两个查询找到合适的构造函数?

UPDATE:

[财务]实体的remaining属性类型为double(不是我的决定)。

我们正在将JPA与休眠一起用作实现。假设我具有以下DTO:公共类SupplierInfoDto {private String vendorName;私人BigDecimal剩余财务; ...

java hibernate jpa hql jpql
4个回答
4
投票

我不确定为什么不能识别BigDecimal ctor,但是您可以重载构造函数


0
投票
class named X  with a constructor that takes two parameters. The types of the parameters from the SELECT clause must match the signature defined in the class.

Syntax for the SELECT clause:

select_clause ::= SELECT [DISTINCT] select_expression
    {, select_expression}*
    select_expression ::=
    single_valued_path_expression |
    aggregate_expression |
    identification_variable |
    OBJECT(identification_variable) |
    constructor_expression
    constructor_expression ::=
    NEW constructor_name ( constructor_item {, constructor_item}* )
    constructor_item ::= single_valued_path_expression |
    aggregate_expression
    aggregate_expression ::=
    { AVG | MAX | MIN | SUM }
    ([DISTINCT] state_field_path_expression) |
    COUNT ([DISTINCT] identification_variable |
    state_field_path_expression |
    single_valued_association_path_expression)

0
投票

为什么不使用java.lang.Number作为构造函数参数,并根据参数的.floatValue()/ doubleValue()创建BigDecimal字段。


0
投票

尝试一下:

© www.soinside.com 2019 - 2024. All rights reserved.