【问题标题】:Hibernate - join un related objectsHibernate - 加入不相关的对象
【发布时间】:2011-05-30 20:18:57
【问题描述】:

我有一个要求,我必须使用 Hibernate HQL 连接两个不相关的对象。 这是示例 POJO 类

class Product{
int product_id;        
String name;
String description;
}

Class Item{
int item_id;
String name;
String description;
int quantity;
int product_id; //Note that there is no composed product object.
}

现在我想执行如下查询 select * from Product p left outer join Item i on p.product_id = i.item_id

我想要一个多维数组作为此查询的输出,这样我就可以拥有 Product 和 Item 的单独实例,而不是一个组合在另一个实例中。 在 Hibernate 中有没有办法做到这一点?

【问题讨论】:

    标签: hibernate hql


    【解决方案1】:

    有一种解决方法可以在 hql here 中加入 id。

    但是,我建议在此处使用本机 sql 查询。

    【讨论】:

    • 从您的链接中引用:“虽然这个 [cross join with ,] 可以很好地编译,但我没有追求这个解决方案,所以不能真正评论它的有效性。我放弃了它,因为它感觉不对。但它应该起作用。”这是你推荐的理由吗? 感觉不对
    • 不,不是。与 hql 合作时,我希望加入关联,而不是 id。
    【解决方案2】:

    通过存储关联对象的id而不是关联对象本身来表示对象之间的关联是不常见的。存储对象更具表现力和类型安全,并且不需要对性能产生影响,因为 hibernate 具有延迟加载代理。

    当然,您可以在映射文件中加入未映射为关联的事物。引用hibernate reference manual

    可以出现多个类,导致 在笛卡尔积或“交叉”中 加入。

    from Formula, Parameter
    
    from Formula as form, Parameter as param
    

    and

    查询可以返回多个对象 和/或属性作为类型数组 对象[]:

    select mother, offspr, mate.name
    from DomesticCat as mother
        inner join mother.mate as mate
        left outer join mother.kittens as offspr
    

    或者作为一个列表:

    select new list(mother, offspr, mate.name)
    from DomesticCat as mother
        inner join mother.mate as mate
        left outer join mother.kittens as offspr
    

    或者 - 假设类 Family 有一个适当的构造函数 - 作为 实际的类型安全 Java 对象:

    select new Family(mother, mate, offspr)
    from DomesticCat as mother
        join mother.mate as mate
        left join mother.kittens as offspr
    

    您可以为选定的指定别名 使用 as 的表达式:

    select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
    from Cat cat
    

    这在一起使用时最有用 选择新地图:

    select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
    from Cat cat
    

    此查询从别名返回 Map 到选定的值。

    将其与一个简单的 where 子句结合,我们得到:

    select product, item
    from Product product, Item item
    where item.product_id = product.product_id
    

    编辑:我错过了你想要一个外部加入。在那种情况下,我不知道除了映射关联之外的其他方法,以便您可以对其进行普通连接。请注意,这不需要特定形式的查询结果,即您仍然可以这样做:

    select product, item
    from Item item
    left join item.product as product
    

    【讨论】:

      【解决方案3】:

      你可以使用子选择

       from Product p where p.product_id in (select item_id from Item)
      

      【讨论】:

      • 1.你的意思是(从项目中选择product_id)不是吗? 2. 这不是外联,没有商品的商品不予退换。 3. 即使 CuriousMind 明确要求,这也不会返回加入的项目。
      • @meriton 1. 不,我的意思是 p.product_id in (select item_id from Item) 因为问题很明确关于 p.product_id = i.item_id 2 和 3。是的,我知道,Item 实例不会被返回,因为他没有使用 fetch 子句。所以我想他只是想检索普通的 Product 实例
      • 1.: 啊,你是对的。可能是问题中的一个错误 - 当他有 product_id 列时,他为什么要加入 item_id ?但是,谁知道呢,也许他真的想要这么奇怪的加入条件……
      【解决方案4】:

      在 select 子句中尝试 subselect:

      select p, (select i from Item where p.product_id = i.item_id) from Product p
      

      【讨论】:

        猜你喜欢
        • 2020-09-03
        • 2016-02-04
        • 2015-08-04
        • 2015-06-09
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多