Leo
Leo

Reputation: 187

Hibernate Joins using HQL

I just need a simple example in HQL (no Criteria) where I have two tables which need to be joined with left outer join on a column which is of type 'int'. I have Sybase at the back-end.

I have looked into a lot places(even on this site) and including the documentation but didn't find them too useful for first time users.

Consider I have Table 'First' and Table 'Second' with the join column 'empID' as 'int'. How can I left outer join First with second using a named query. Please also specify if any additional mapping changes are required to the hbm files of First and Second class. I am sure it will help simplify joins for a lot of Hibernate newbies.

EDIT:

empID is a value which is definitely present in table First and may or may not be present in table Second. It just refers to an employee.

Upvotes: 0

Views: 2194

Answers (2)

Firo
Firo

Reputation: 30813

select ... from First as f, Second as s where f.employee.Id = s.employee.Id

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692151

It's not possible. Joins are only possible if there is an association between two entities.

You could be able to do what you want if you had:

  • a ManyToOne or OneToOne association from First to Employee
  • a OneToMany or OneToOne association from Employee to Second

and in this case, the query would look like

select first from First first
inner join fetch first.employee employee
left join fetch employee.second(s) second
where ...

Upvotes: 1

Related Questions