一个真实的生产场景。背景:6个表:资金,账户,期间,期间权重,持有,头寸。
Fund: The fund information, for example: fund name, total Asset, start time.. etc.
Account: each fund has an account (one to one) to fund.
period: time period ( for example: 2000-01-01 to 2000-12-31)
periodweight: at a certain period, the target holding weight.
holding: IBM, Oracle, GE are stock holdings.
position: IBM($3000), oracle($2000), GE($5000)如果我有一个基金名称:fake fund,它的目标持有量为IBM (30%)、Oracle(20%)、GE(50%)这段时间( 2000-01-01至2000-12-31),2000-01-01的实际持仓量为10%、10%、%80%,而2000-01-02的实际持仓量为20%、20%、60%,如表中所示
Account: id account_Number Fund_id
* 1 0001 10
2 0002 11
Fund: id name other properties...
* 10 fake fund xxx
11 another fake one xxx
period: id start_time end_time fund_id
* 3 2000-01-01 2000-12-31 10
4 2001-01-01 2001-12-31 10
periodWeight: id target_weight holding_id period_id
*11 30% 21 3
*12 20% 22 3
*13 50% 23 3
holding: id name order other properties...
*21 IBM 1 xxx
*22 Oracle 2 xxx
*23 GE 3 xxx
position: id Account_id holding_id date actual_position
1 1 11 2000-01-01 10%
2 1 12 2000-01-01 10%
3 1 13 2000-01-01 80%
4 1 11 2000-01-02 20%
5 1 12 2000-01-02 20%
6 1 13 2000-01-02 60%java类包括
Account{
@onetoOne(mappedby="account")
Fund f;
@oneToMany
Set<Position> positions;
}
Fund{
@manyToOne
Account account;
@oneToMany(mappedby="fund")
Set<Period> periods;
}
Period{
@manyToOne
Fund fund;
@oneToMany(mappedby="period")
Set<PeriodWeight> periodWeights;
}
PeriodWeight{
@manyToOne
Period period;
@ManyToOne
Holding holding
}
Holding{
@OneToMany(mappedby="holding")
Set<PeriodWeight> periodWeights;
@OneToMany
Set<Position> positions;
}
Position{
@manyToOne
Account account;
@manyToOne
Holding holding;
}我想要一个查询:基于日期(2000-01-01)和基金名称(假基金)。我想构建一个基金对象,它包含account和period(2000-01-01到2000-12-31),period包含periodWeight,periodWeight包含holding,holding包含(2000-01-01)的位置。当没有这样的位置时,例如,我查询2000-01-03和假基金,我想要有结构,只是位置在持有中是一个空的集合。
如果有数据,这个hql可以正确地加载结构。
select f from Fund f
inner join fetch f.account a
inner join fetch f.period p
inner join fetch p.periodWeight w
inner join fetch w.holding h
inner join fetch h.positions po
where f.name=:name and :date between p.start_date and p.end_date and :date=po.date and po.account= a问题是,当position表中没有当天的数据时,它将返回null。我需要一个sql来给我没有数据时的结构,它可以加载除位置之外的所有内容,只需将位置设置为空即可。
另一个问题是,加载如此复杂的结构的更好方法是什么?像这样的一个hql,或者通过一个hql加载结构的一部分,然后通过另一个hql加载其他部分?因为在sql中,你总是一个接一个地加载它们,首先是资金,然后是期间,然后是权重,然后是持有,然后是头寸等等。权重需要根据持有顺序进行排序。
select f from Fund f
inner join fetch f.account a
inner join fetch f.period p
inner join fetch p.periodWeight w
inner join fetch w.holding h
left join fetch h.positions po with po.account= a and :date=po.date
where f.name=:name and :date between p.start_date and p.end_date是非常接近的东西,但这给了我错误,
org.hibernate.hql.ast.QuerySyntaxException: with-clause not allowed on fetched associations; use filters发布于 2013-07-09 19:36:24
这是HQL中令人讨厌的限制,我在基于时间的关联中遇到过几次。
我发现这是对包含fetch和with子句的HQL查询的解决方案:
select f from Fund f
inner join fetch f.account a
inner join fetch f.period p
inner join fetch p.periodWeight w
inner join fetch w.holding h
left join fetch h.positions po
where f.name=:name and :date between p.start_date and p.end_date
and (po is null or (po.account= a and :date=po.date))诀窍是将with子句移到where子句,并添加一个将对象设置为null的选项,从而允许它返回不带位置的行。
发布于 2011-08-31 23:00:54
您应该能够通过更改以下内容来完成此操作:
inner join fetch h.positions p至:
left join fetch h.positions p和调用
query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);在执行查询之前,在query对象上。
https://stackoverflow.com/questions/7252907
复制相似问题