-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to use DetachedCriteria in multiple table?
PostPosted: Tue Nov 24, 2009 6:07 am 
Newbie

Joined: Sat Nov 07, 2009 12:08 pm
Posts: 3
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:

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);

if(!StringUtils.isEmpty(studentName)) {
      criteria.add(Restrictions.eq("studentName", studentName));
}


// Suspect line
if(!StringUtils.isEmpty(departmentName)) {
      criteria.createCriteria("department")
      .add(Restrictions.eq("departmentName", departmentName));
}

// Suspect line
if(!StringUtils.isEmpty(departmentCode)) {
      criteria.createCriteria("department")
      .add(Restrictions.eq("departmentCode", departmentCode));
}

return getHibernateTemplate().findByCriteria(criteria);


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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.