简体   繁体   English

@OneToMany上的多个联接,每个联接都有条件

[英]multiple join on @OneToMany with condition on each

I have the following structure: 我有以下结构:

Product.Java : 产品Java:

String name

@OneToMany(mappedBy="product")
private List<ProductServingTimeAvailablity> servingTimeAvailablities;

@OneToMany(mappedBy="product")
private List<ProductStyles> styles;

@OneToMany(mappedBy="product")
private List<ProductLocationAvailability> locationAvailabilites;

I need to retrieve all the products where 我需要在哪里检索所有产品

  • product.servingTimeAvailablities contains 1 and product.servingTimeAvailablities包含1和
  • product.styles contains 2 and product.styles包含2和
  • product.locationAvailabilites contains 3 product.locationAvailabilites包含3

(meaning - each product can have many time availability and many styles and many locations and I need to choose only the products that are available at time 1, are style 2 and are available for location 3) (意思是-每个产品都可以有很多时间可用性,许多样式和位置,我只需要选择在时间1可用的产品,样式2和在位置3可用的产品)

How can I do it in HQL? 如何在HQL中完成?

It's not in but contains. 它不在但包含。

Something like the following should work. 像下面这样的东西应该起作用。

SELECT product FROM Product product
    join product.servingTimeAvailablities as servingTimeAvailablities
    join product.styles as styles
    join product.locationAvailabilites as locationAvailabilites
    WHERE servingTimeAvailablities.someProperty = :somePropertyValue1
    AND styles.someProperty = :somePropertyValue2
    AND locationAvailabilites.someProperty = :somePropertyValue3

I don't know the internals of your objects, which is why I used 'someProperty' as a field name. 我不知道您的对象的内部,这就是为什么我使用“ someProperty”作为字段名称的原因。 You should replace it with the proper field name that you want to match. 您应该将其替换为要匹配的正确字段名称。 I also used 'join' as the join type for the example. 我还使用“ join”作为示例的连接类型。 You should replace that with the join type that you want to use (eg inner join, left outer join, etc). 您应将其替换为要使用的联接类型(例如,内部联接,左外部联接等)。

Let me know if that works for you. 让我知道这是否适合您。 I Haven't tried it myself :) 我自己还没有尝试过:)

References that helped me were: 对我有帮助的参考文献是:

http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/queryhql.html#queryhql-select http://docs.jboss.org/hibernate/orm/3.6/reference/zh-CN/html/queryhql.html#queryhql-select

HQL: illegal attempt to dereference collection HQL:非法尝试取消引用集合

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

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