如何在 JPQL 中使用别名
How to work with alias in JPQL
我正在尝试从 H2 数据库中获取一些值 table。
执行我需要的查询是这样的:
SELECT cast(creationDate as date) as DATE, SUM(paymentValue) as TOTAL,fxRate
FROM payment
group by DATE
其中 "creationDate"、"paymentValue"、"fxRate" 是 table "payment" 的列。
CreationDate 是一个时间戳,所以我只需要从中获取日期。
当我尝试将其写入 Java
时
@Query("SELECT cast(creationDate as date) as daydate , SUM(paymentValue) as value1, fxRate as value2 FROM payment " +
"group by cast(creationDate as date)")
List<Payment> findPaymentValuePerDay ();
我收到错误 [Ljava.lang.Object; cannot be cast to ...entity.Payment.
我还尝试使用另一个名为 GraphDto 的对象,它具有属性 daydate、value1 和 value2
@Query("SELECT cast(creationDate as date) as daydate , SUM(paymentValue) as value1, fxRate as value2 FROM payment " +
"group by cast(creationDate as date)")
List<GraphDto> findPaymentValuePerDay ();
但我得到了同样的错误。
[Ljava.lang.Object; cannot be cast to ...entity.GraphDto.
那么,如何在 JPQL 中使用别名??我只需要一个函数,该函数 returns 3 个不同的列的名称以及使用正确的 H2 查询从现有实体中获取的值。
谢谢大家
你的查询 return 一个 Object[]
而不是 GraphDto
对象的数组,你有多种方法来解决这个问题:
解决方案 1
创建一个包含 daydate
、value1
、value2
的构造函数
@Entity
public class GraphDto{
private Date daydate;
private Long value1;
private Long value2;
public GraphDto(Date daydate, Long value1, Long value2){
//...
}
//..getters and setters
}
那么您的查询应该如下所示:
SELECT NEW com.packagename.GraphDto(cast(creationDate AS date), SUM(paymentValue), fxRate)
FROM payment
GROUP BY cast(creationDate AS date)
解决方案 2
将 return 类型更改为:
List<Object[]> findPaymentValuePerDay ();
然后在您的服务中循环遍历此对象并提取值:
List<Object[]> listObject = rep.findPaymentValuePerDay();
for(Object[] obj : listObject){
Date date = (Date) obj[0];
Long value1 = (Long) obj[1];
Long value2 = (Long) obj[2];
}
我正在尝试从 H2 数据库中获取一些值 table。 执行我需要的查询是这样的:
SELECT cast(creationDate as date) as DATE, SUM(paymentValue) as TOTAL,fxRate
FROM payment
group by DATE
其中 "creationDate"、"paymentValue"、"fxRate" 是 table "payment" 的列。 CreationDate 是一个时间戳,所以我只需要从中获取日期。 当我尝试将其写入 Java
时 @Query("SELECT cast(creationDate as date) as daydate , SUM(paymentValue) as value1, fxRate as value2 FROM payment " +
"group by cast(creationDate as date)")
List<Payment> findPaymentValuePerDay ();
我收到错误 [Ljava.lang.Object; cannot be cast to ...entity.Payment.
我还尝试使用另一个名为 GraphDto 的对象,它具有属性 daydate、value1 和 value2
@Query("SELECT cast(creationDate as date) as daydate , SUM(paymentValue) as value1, fxRate as value2 FROM payment " +
"group by cast(creationDate as date)")
List<GraphDto> findPaymentValuePerDay ();
但我得到了同样的错误。
[Ljava.lang.Object; cannot be cast to ...entity.GraphDto.
那么,如何在 JPQL 中使用别名??我只需要一个函数,该函数 returns 3 个不同的列的名称以及使用正确的 H2 查询从现有实体中获取的值。 谢谢大家
你的查询 return 一个 Object[]
而不是 GraphDto
对象的数组,你有多种方法来解决这个问题:
解决方案 1
创建一个包含 daydate
、value1
、value2
@Entity
public class GraphDto{
private Date daydate;
private Long value1;
private Long value2;
public GraphDto(Date daydate, Long value1, Long value2){
//...
}
//..getters and setters
}
那么您的查询应该如下所示:
SELECT NEW com.packagename.GraphDto(cast(creationDate AS date), SUM(paymentValue), fxRate)
FROM payment
GROUP BY cast(creationDate AS date)
解决方案 2
将 return 类型更改为:
List<Object[]> findPaymentValuePerDay ();
然后在您的服务中循环遍历此对象并提取值:
List<Object[]> listObject = rep.findPaymentValuePerDay();
for(Object[] obj : listObject){
Date date = (Date) obj[0];
Long value1 = (Long) obj[1];
Long value2 = (Long) obj[2];
}