Hibernate - HQL to fetch a collection from Unidirectional OneToMany relationship

后端 未结 2 1298
自闭症患者
自闭症患者 2021-02-08 23:14

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

public class Order {
    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name=\"o         


        
相关标签:
2条回答
  • 2021-02-08 23:41
    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.

    0 讨论(0)
  • 2021-02-08 23:45

    It sounds like you want to map the other side of this relationship i.e. make this bi-directional mapping.

    Then you can use the order number in your HQL:

    from Item as item where item.amount > :amount and item.order.id = :orderId
    

    From the HQL documentation:

    from Cat as cat where cat.mate.id = 69 The query is efficient and does not require a table join.

    The fact that you are trying to do this query suggests that the relationship between a Line and its Order is important!

    0 讨论(0)
提交回复
热议问题