【问题标题】:multiple join on @OneToMany with condition on each@OneToMany 上的多个连接,每个都有条件
【发布时间】:2013-08-30 13:17:23
【问题描述】:

我有以下结构:

产品.Java:

String name

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

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

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

我需要检索所有的产品

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

(意思是 - 每个产品可以有很多时间可用性和许多样式和许多位置,我只需要选择在时间 1 可用、样式 2 并且在位置 3 可用的产品)

如何在 HQL 中做到这一点?

它不在但包含。

【问题讨论】:

    标签: java mysql hibernate hql


    【解决方案1】:

    类似下面的东西应该可以工作。

    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
    

    我不知道您的对象的内部结构,这就是我使用“someProperty”作为字段名称的原因。您应该将其替换为您想要匹配的正确字段名称。我还使用 'join' 作为示例的连接类型。您应该将其替换为您要使用的连接类型(例如内连接、左外连接等)。

    让我知道这是否适合您。我自己没试过:)

    对我有帮助的参考资料是:

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

    HQL: illegal attempt to dereference collection

    【讨论】:

    • 谢谢,我只想要产品表数据。我应该使用什么连接?
    • 在您的情况下,我相信“内部连接”是您需要的。我上面提供的示例使用了内部联接(请注意,您可以通过在 HQL 中编写 'join' 来缩写 'inner join',就像我在示例中所做的那样)。请参阅docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/… 了解更多信息。
    • 执行类似于我在答案中提供的示例的查询应该会为您提供符合指定条件的所有产品。仅供参考:如果您已经熟悉 SQL,也许以下参考资料可能有助于理解 SQL 中存在的不同类型的连接:stackoverflow.com/questions/38549/…
    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    相关资源
    最近更新 更多