首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hibernate,如何在现实世界中加载复杂对象

hibernate,如何在现实世界中加载复杂对象
EN

Stack Overflow用户
提问于 2011-08-31 13:30:18
回答 2查看 3.7K关注 0票数 6

一个真实的生产场景。背景:6个表:资金,账户,期间,期间权重,持有,头寸。

代码语言:javascript
复制
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%,如表中所示

代码语言:javascript
复制
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类包括

代码语言:javascript
复制
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可以正确地加载结构。

代码语言:javascript
复制
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中,你总是一个接一个地加载它们,首先是资金,然后是期间,然后是权重,然后是持有,然后是头寸等等。权重需要根据持有顺序进行排序。

代码语言:javascript
复制
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

是非常接近的东西,但这给了我错误,

代码语言:javascript
复制
org.hibernate.hql.ast.QuerySyntaxException: with-clause not allowed on fetched associations; use filters
EN

回答 2

Stack Overflow用户

发布于 2013-07-09 19:36:24

这是HQL中令人讨厌的限制,我在基于时间的关联中遇到过几次。

我发现这是对包含fetchwith子句的HQL查询的解决方案:

代码语言:javascript
复制
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的选项,从而允许它返回不带位置的行。

票数 2
EN

Stack Overflow用户

发布于 2011-08-31 23:00:54

您应该能够通过更改以下内容来完成此操作:

代码语言:javascript
复制
inner join fetch h.positions p

至:

代码语言:javascript
复制
left join fetch h.positions p

和调用

代码语言:javascript
复制
query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

在执行查询之前,在query对象上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7252907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档