0

塔布斯:

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) 的 Grandpa 对象。这是我的,但它不起作用:

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
4

1 回答 1

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

但要小心巨大的笛卡尔积

于 2012-09-19T10:00:03.443 回答