Home Ask Login Register

Developers Planet

Your answer is one click away!

Asier Aranbarri February 2016

HQL Join with three tables

I'm having some issues with HQL since I'm a newbie with it. Even though I don't have issues with "simple queries", I am currently stuck with a query between three tables.

I already read the tutorials, but I can't find a valid example for my needs. I will do my best to explain my problem:

I have three different tables, let's name them HOUSES, OWNERS, and OWNERINFOS.

Given a town Id, I need to list all houses from that town, including the name and surname of that house's owner.

I made a really simple graph to show the connections between tables:

enter image description here

I'm also unsure on which join strategy I should use. Any kind of help would be highly appreciated, since solving this is a priority for me.

Thanks in advance!


v.ladynev February 2016

It is just a template

class House {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_town")
    private Town town;

    @OneToMany(mappedBy = "house")
    private List<Owner> owners;


class Owner {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_house")
    private House house;

    @JoinColumn(name = "fk_owner_info")
    private OwnerInfo ownerInfo;


class OwnerInfo {

    @OneToOne(mappedBy = "ownerInfo", fetch = FetchType.LAZY)
    private Owner owner;


The simplest case with fetch all owners with owner info

from House h inner join fetch h.owners where h.town.id = :townId

Post Status

Asked in February 2016
Viewed 2,879 times
Voted 14
Answered 1 times


Leave an answer

Quote of the day: live life