Hibernate HQL Select Query Based on Item in a One-to-Many Relationship

Few days back I had a situation where I have to fetch some data based on a value of an item stored in one-to-many relationship. I my project I had two classes like below. I had to fetch all the Oders that has particular item.

class Order {
    private List<Item> items;
    //more code ...

class Item {
    private String itemId;
    private String name;
    //more code ...

With native SQL it was not that difficult, but since I've used Hibernate I wanted to find a way to do it in HQL. After some searching in Google I found out how to do it.

select o from Order o left join o.items its where its.itemId = :itemId

Reader Comments

Would be better to use an inner join as opposed to a left outer join.

Tribune to Heros

Tribune to Heros
We will never forget the sacrifices you made to protect our country....


Get Google Toolbar Button

Enter your email address:

Delivered by FeedBurner