如何为一对多关系形成HQL查询并将一对多分组

时间:2017-07-19 06:26:43

标签: hibernate jpa spring-data spring-data-jpa hql

我需要生成hql查询。我的表结构如下所示 A -----一对多------>乙

和我的oracle db query

select a.CONF_NUMBER,to_char(max(b.RECEIVE_DATE), 'YYYY-MM-DD hh24:mi:ss')
from A a, B b
where b.RECEIVE_DATE >= to_date('2012-06-07 13:20:28','YYYY-MM-DD hh24:mi:ss') 
group by a.CONF_NUMBER

这里我对如何应用max(b.RECEIVE_DATE)的事实感到困惑 对于子查询如何在HQL中工作而感到困惑

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

DTO

public interface NumAndDate {
    String getNumber();
    Date getDate();
}

<强>存储库

public interface ARepository extends JpaRepository<A, Long> {

     @Query("select a.confNumber as number, max(b.receiveDate) as date from A a join a.b b where b.receiveDate >= ?1 group by a.confNumber")
     List<NumAndDate> getDataByDate(Date date);
}

JPQL Language Reference

中查看更多信息