Larisa
Larisa

Reputation: 73

How to use Hibernate Criteria for joining two tables with OneToMany relation

I have two tables for which I can't implement mapping @OneToMany in their entities unless I have a circle dependency. I want to create a Hibernate Criteria so that I'd be able to join this tables on ids' fields plus a restriction. However, I have a sql query which gives me a result I'm looking for, I failed to understand how to implement the criteria.

Tables which have a shared id:

|  Table A  | Table B |
|id         | id      |
|languageId | code    |
|comment    |         |

SQL query:

select a.id, a.languageId, a.comment, b.code from TableA a join TableB b on a.id=b.id

Could anybody help me to write the Hibernate Criteria?

Thank you in advance, L.

Upvotes: 1

Views: 1115

Answers (1)

axtavt
axtavt

Reputation: 242726

You cannot do it - Hibernate Criteria doesn't support join on arbitrary conditions.

You have to use HQL query (it doesn't support JOIN syntax with arbitrary condition too, but you can use old-style syntax):

SELECT a, b FROM A a, B b WHERE a.id = b.id

Upvotes: 2

Related Questions