Talbes:
Grandpa
GP_ID (PK)
NAME
Dad
DAD_ID (PK)
PG_ID (FK)
Children
C_ID (PK)
DAD_ID (FK)
MONEY_AMOUNT
MONEY_UNIT
类(此处省略注释):
class Grandpa
{
Long id;
String name;
Double moneyAmount; // sum of Children's money amount
String moneyUnit; // first of Children's money unit.
}
class Dad
{
Long id;
Grandpa grandpa;
}
class Children
{
Long id;
Dad dad;
Double moneyAmount;
String moneyUni;
}
我想编写一个hql来获取带有sum(grandpa.dad.children.money_amount)和first(grandpa.dad.children.money_unit)的爷爷对象。这是我的,但它不起作用:
select gp, sum(chdn.moneyAmount) as gp.moneyAmount, first(chdn.moneyUnit) as gp.moneyUnit
from Grandpa gp, Dad dad, Children chdn
where gp.id =: chdn.dad.grandpa.id
答案 0 :(得分:1)
select gp.id, gp.name, sum(chdn.moneyAmount) as moneyAmount, min(chdn.moneyUnit) as gp.moneyUnit
from Children chdn, chdn.dad dad, dad.grandpa gp
groupBy gp.Id
并使用aliastobean变换器投射到爷爷dto
或者你实现moneyAmount和unit作为访问包含爸爸的子集合的属性,并将内存中的金额相加然后它将是
select gp from Grandpa gp join fetch gp.children dad join fetch dad.children
但要注意巨大的笛卡儿产品