aspose file tools
The moose likes Object Relational Mapping and the fly likes How to use DetachedCriteria in multiple table? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to use DetachedCriteria in multiple table?" Watch "How to use DetachedCriteria in multiple table?" New topic

How to use DetachedCriteria in multiple table?


Joined: Dec 09, 2007
Posts: 3
I have two table here:

- StudentId
- StudentName

- DepartmentName
- DepartmentCode
- StudentId

I doing searching function in between this two table for Student

User can Query StudentName, DepartmentCode, DepartName ( 3 of them is textfield ).

Here is the Query:

The problem is
When I search departmentName and departmentCode together , it will output departmentName result only.

So far I know when criteria.createCriteria("department") the query will go to department table and searching ( something like table linking) But this cannot work for using it more than one time.

Can anyone there pin point my mistake?


Mark Spritzler

Joined: Feb 05, 2001
Posts: 17268

DetachedCriteria is mostly used to define a subquery, and for what you need is a bit overkill.

Just create the Criteria based on the object that would have been first in your HQL version, then just addAlias(OtherObject.class, "aliasNameHere"). Now you can use the alias name in your Projections and Restrictions.

To get you started.

Criteria criteria = session.createCriteria(Student.class).addAlias(Department.class, "dept").add(Restriction.eq(dept.departmentName, someValue));


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
It is sorta covered in the JavaRanch Style Guide.
subject: How to use DetachedCriteria in multiple table?