相关疑难解决方法(0)

JPA/Hibernate + HQL/JPQL:选择带有 BigDecimal 参数的 DTO

我们使用带有休眠的 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
}
Run Code Online (Sandbox Code Playgroud)

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

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

但是,我得到了一个 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
Run Code Online (Sandbox Code Playgroud)

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

更新:该 …

java hibernate jpa hql jpql

2
推荐指数
1
解决办法
6430
查看次数

标签 统计

hibernate ×1

hql ×1

java ×1

jpa ×1

jpql ×1