• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Easy SQL into Hibernate Criteria

 
Eric Juneau
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a criteria object, but one of the criteria is complicated. I want to make one of the criterion "last name", but the table doesn't have a last name. It does have a foreign key ID to the table that does. So I'd like to insert some sql into the criterion, but I can't find an example of this. It also needs to allow other "normal" criterion into the criteria (example: criteria.add( Restrictions.ilike( "activity_type", activityTypeId, MatchMode.EXACT ) ); )

Here's the sample SQL of what I was planning:
SELECT account.*
FROM account, user
WHERE account.user_id = user.user_id
AND user.last_name like :variable
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just create an instance of the associated object and initialize the name property and add it to the criteria query. Don't try mixing the HQL and criteria. That will over-complicate things greatly.

Your criteria doesn't all have to go to one table.

-Cameron McKenzie
 
Eric Juneau
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The account object doesn't hold onto any instances of user (i.e., there's no "list of users" in an account object - they're separate entities with no relation as far as the app's concerned), so I don't think your solution would work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic