This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Need Hibernate Criteria help. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Need Hibernate Criteria help." Watch "Need Hibernate Criteria help." New topic
Author

Need Hibernate Criteria help.

Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
Hi,


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 t2.id=val;

here val is input from the user.

Table1 :
CID,
data

Table2:
CID
ID



pls pls pls pls pls

Thanks
Praveen Kumar
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4967

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 ]

Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
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: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Need Hibernate Criteria help.
 
Similar Threads
Hibernate: Different Join in Save() and Get()
how to get child table values from parent query
Convert to hibernate simple SQL statement
DELETE using subquery
Getting one Row from JOIN query