Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Object Relational Mapping and the fly likes complex query in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "complex query in hibernate" Watch "complex query in hibernate" New topic

complex query in hibernate

Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

I am looking for writing a complex query in hibernate. Can anybody provide me useful tutorial.

The scenario is:
1> I have to get the list of data(ie.object) from 1 table using where clause. and then for eve

2> Then for every item in the list I have to get the list of data(ie. object) from 2nd table.

Both tables are connected by foreign key relationship.

Is it possible to write a single query instead of 2 queries so that I can get desired result from second table directly.
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10403

I guess, you can do the following:

Have an hibernate object(HO) XyzHO mapped to table XYZ and an hibernate object AbcHO mapped to table ABC.

Your XyzHO would look like:

public class XyzHO {

//set of AbcHO objects
Set abcHOs;


public class AbcHO {

//reference to the XyzHO
XyzHO xyzHO;


You would then map these two objects to the tables using, hbm files.

Then your query would look something like:

"from XyzHO where place your where clause"

This would return you a list of XyzHO objects and each XyzHO object would contain a collection of AbcHO objects.

[My Blog] [JavaRanch Journal]
subject: complex query in hibernate