Hibernate - HQL从单向OneToMany关系获取集合 [英] Hibernate - HQL to fetch a collection from Unidirectional OneToMany relationship

查看:586
本文介绍了Hibernate - HQL从单向OneToMany关系获取集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class Order {
@ OneToMany(cascade = CascadeType.ALL)
@JoinTable(name =order_item,joinColumns = {@ JoinColumn(name =order_id)},inverseJoinColumns = {@ JoinColumn(name =item_id)})
public Set< Item> getItems(){
返回项目;


正常情况下,获取此订单的内容很简单:

 列表<项目> items = order.getItems(); 

但是无论出于何种原因,我可能想要以某种方式过滤我的结果并仅检索部分集合的项目,如所有的项目超过一定的价格,低于某些股票等尽可能最快的方式(不返回,然后所有,然后过滤)。要做到这一点,我会运行一个HQL查询来检索特定订单的商品,并在我的where子句或我的查询对象中添加更多的东西。



直觉上我会想要这样的行为(这是完全错误的):

pre $ 从join_tablem中选择jointable.ITEM作为jointable inner join jointable.order where order = :订单

但当然这是错误的,因为HQL根据映射实体工作,所以我不能使用查询中的连接表。那么做到这一点的正确方法是什么?



编辑:

我已经找到了这个问题的答案,我想要以下查询:

 从Order o中选择o.items其中o =? 

这使我可以获取订单项目的集合,而无需使用双向关系。我现在对这个问题的第二个阶段感到困惑,那就是如何过滤这个集合的结果,最简单的例子是:

 从Order o中选择o.items,其中o =? order by o.items.someumberfield asc 

哪个返回非法尝试解引用集合,那么如何过滤我的物品?
$ b

编辑:

票证解决方案实际上是正确的,

解决方案

 从订单中选择商品
内部连接order.items item
where order =:order
and ...

HQL查询使用实体及其关联。关联使用连接表的事实对于HQL并不重要:您可以通过关联进行导航,Hibernate会对SQL进行相应的转换。


I have an Class with a unidirectional one to many relationship as follows:

public class Order {
    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name="order_item", joinColumns={@JoinColumn(name="order_id")}, inverseJoinColumns={@JoinColumn(name="item_id")})
    public Set<Item> getItems() {
        return items;
    }
} 

Normally getting the contents of this order is straightforward:

List<Item> items = order.getItems();

But for whatever reason I might want to filter my results in some way and retrieve only part of a collection of items such as all items more than a certain price, below a certain stock etc in the fastest way possible (Not returning then all then filtering afterwards). To do this I would run a HQL query to retrieve the items for a particular order and add some more stuff into my where clause or onto my query object.

Intuitively I would want this behaviour (Which is completely wrong):

SELECT jointable.ITEM from order_item as jointable inner join jointable.order where order = :order

But of course this is wrong as HQL works in terms of mapped entities, so I cannot use the join table in the query. So what is the correct way to do this?

Edit:

I have found the answer to this question, I want the following Query:

Select o.items from Order o where o = ?

This allows me to fetch the collection of items for an order, without having to use a bidirectional relationship. I am now however confused on the second stage of this question, which is how to filter the results of this collection, the most simple example being:

Select o.items from Order o where o = ? order by o.items.somenumberfield asc

Which returns illegal attempt to dereference collection, so how would I filter my items?

Edit:

The ticket solution is actually correct, I misread the solution originally.

解决方案

select item from Order order
inner join order.items item
where order = :order
and ...

HQL queries use entities and their associations. The fact that the association uses a join table or not is not important for HQL: you navigate through associations and Hibernate does the appropriate translation to SQL.

这篇关于Hibernate - HQL从单向OneToMany关系获取集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆