• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Easy SQL into Hibernate Criteria

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic