繁体   English   中英

使用“联接”获取模式时,如何停止休眠返回多个实例的休眠?

[英]How do I stop hibernate returning multiple instances when using “join” fetch mode?

鉴于以下情况,当我执行查询时,我试图强制加载子集合(countryData),这是可行的,但是最后我却加载了Bin记录的副本。

public Collection<Bin> getBinsByPromotion(String season, String promotion) {
    final Session session = sessionFactory.getCurrentSession();
    try {
        session.beginTransaction();
        return (List<Bin>) session.createCriteria(Bin.class).
                setFetchMode("countryData", FetchMode.JOIN).
                add(Restrictions.eq("key.seasonCode", season)).
                add(Restrictions.eq("key.promotionCode", promotion)).
                add(Restrictions.ne("status", "closed")).
                list();
    } finally {
        session.getTransaction().commit();
    }
}

我不希望使用默认(懒惰)行为,因为查询将返回〜8k条记录,因此发送了16k条额外的查询以获取子记录。

如果没有别的我更喜欢。

select ... from bins b where b.seasonCode = ?
                         and b.promotionCode = ?
                         and b.status <> 'Closed';
select ... from binCountry bc where bc.seasonCode = ?
                                and bc.promotionCode = ?;

您可以使用CriteriaSpecification.DISTINCT_ROOT_ENTITY;

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM