• 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

How to use DetachedCriteria in multiple table?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two table here:

Student
- StudentId
- StudentName

Department
- 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?

Thanks
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));

Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic