How do I left join tables in unidirectional many-to-one in Hibernate?

Posted by jbarz on Stack Overflow See other posts from Stack Overflow or by jbarz
Published on 2010-03-04T23:18:22Z Indexed on 2010/03/14 1:35 UTC
Read the original article Hit count: 296

I'm piggy-backing off of http://stackoverflow.com/questions/2368195/how-to-join-tables-in-unidirectional-many-to-one-condition.

If you have two classes:

class A {
    @Id
    public Long id;
}

class B {
    @Id
    public Long id;
    @ManyToOne
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    public A parent;
}

B -> A is a many to one relationship. I understand that I could add a Collection of Bs to A however I do not want that association.

So my actual question is, Is there an HQL or Criteria way of creating the SQL query:

select * from A left join B on (b.parent_id = a.id)

This will retrieve all A records with a Cartesian product of each B record that references A and will include A records that have no B referencing them.

If you use:

from A a, B b where b.a = a

then it is an inner join and you do not receive the A records that do not have a B referencing them.

I have not found a good way of doing this without two queries so anything less than that would be great.

Thanks.

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about many-to-one