It's not a secret anymore!
The moose likes Object Relational Mapping and the fly likes Need Hibernate Criteria help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Need Hibernate Criteria help." Watch "Need Hibernate Criteria help." New topic

Need Hibernate Criteria help.

Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133

Could any one help me how to write the below HQL in Hibernate criteria....

Select t1 from table1 t1, table2 t2 where t1.CID= t2.CID and;

here val is input from the user.

Table1 :


pls pls pls pls pls

Praveen Kumar
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

No need for the pls pls pls.

The first thing you need is a mind shift. So, you've got tablea and tableb and tablec? How does that matter? I could have one class mapped across all three tables. Or ten-thousand classes mapped to those three tables? You are thinking about tables. Sure, they are the underpinning of your data, but Hibernate and the Criteria API is all about your OBJECTS.

What classes to you have?

TableA and TableB? Maybe a User and an Address? Maybe a Course and Students? What are the OBJECTS?

Then, when you have the objects, you can create your criteria query.

So, a user has an address, and you want to final all Users that live in Toronto? Do this:

Now, do a criteria Example query based on the User. Since the example provided has a city address of Toronto, you'll get back all users that live in Toronto.

What are the names of the underlying tables in this User-Address example, and how many tables are there? Well, there are actually 5000 tables! Yeah, 5000! But we don't care, because that is all mapped behind the scenes by crazy database admins. All we care about is the OBJECTS and their relationships.

Here's an example of some Criteria code, just to give you an idea of how it might work:

If you're interested, you can find more in this tutorial of mine:

Working with the Hibernate Criteria API: Examples and Tutorials

Good luck!

-Cameron McKenzie
[ May 27, 2008: Message edited by: Cameron Wallace McKenzie ]
Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
Hi Cameron,

Thanks for your reply.

I mean tables in the sense i have two classes.

Assume that tables are classes....

Could you tell how to write criteria to join the tables/classes
for above issues ?
I agree. Here's the link:
subject: Need Hibernate Criteria help.
It's not a secret anymore!