Klaus Nji
Klaus Nji

Reputation: 18847

hibernate criteria API with multiple left inner joins

The table relationships is as follows:

I will like to select all users older than 18 years with address type set to 1. Hibernate mappings for User and Address entities exist but there is no associations defined within the mapping from User to Address. The only link between a User and Address is specified via the user_address_associations table. Also, the User class does not defined an Addresses property.

The raw SQL will be:

select * from users 
inner join user_address_associations   
on user_address_associations.userid=users.identity
inner join addresses 
on user_address_associations.addressid=addresses.identity and addresses.type=1
where db.users.age >= 18

Using the Hibernate 3.6.5 Criteria API, I started with this:

Criteria criteria = session.createCriteria(User.class);

// how to do the join to addresses table through user_address_associations table?

// where clause
criteria.add(Restrictions.gt("Age", 1176));

Question is, how do I formuate the joins?

Upvotes: 1

Views: 1466

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

It's impossible. Hibernate queries, (HQL or Criteria) only use entities, their persistent fields and their associations. If the join table is not mapped and is not used to define an association between ther User and Address entities, you won't be able to define queries using this table with Hibernate, except SQL queries of course.

Upvotes: 2

Related Questions