Hi Everybody,
I have some code that takes a domain model creates a Criteria then adds an Example created with the model. Then it looks for any properties in the model that are domain objects and adds subCriteria to the criteria. It does this recursively. Everything works fine except that the id property is not included...
CODE:
Code:
@SuppressWarnings("unchecked")
public List<T> search(T example) {
if (log.isDebugEnabled())
log.debug("entering 'search' method...");
Criteria crit = getSession().createCriteria(persistentClass);
return loadCriteria(crit,example).list();
}
private Criteria loadCriteria(Criteria crit, BaseAuditable example){
// create and add an Example
crit.add(Example.create(example).excludeZeroes().ignoreCase().enableLike(MatchMode.ANYWHERE));
// iterate over the BaseAuditable properties
// create Criteria for each none null BaseAuditable
// call this recursively
try
{
PropertyDescriptor [] descriptors = PropertyUtils.getPropertyDescriptors(example);
for (PropertyDescriptor descriptor: descriptors)
{
if (BaseAuditable .class.isAssignableFrom(descriptor.getPropertyType()))
{
Method getter = descriptor.getReadMethod();
BaseAuditable property =(BaseAuditable)getter.invoke(example, (Object[])null);
if (property != null)
{
log.debug("adding " + descriptor.getName() + " to Critieria.");
loadCriteria(crit.createCriteria(descriptor.getName()), property);
}
}
}
}
catch (Exception e)
{
throw new IllegalStateException(e);
}
log.debug(crit);
return crit;
}
THE CALL:
Code:
Issue example = new Issue();
// set filtering criteria
example.setVisit(new Visit());
example.getVisit().setId(visit.getId());
example.getVisit().setNumMedicalDoctorAttendees(100);
// get filtered issues
List issues = walkRoundsIssueManager.search(example);
LOG:
Quote:
DEBUG [btpool0-1] GenericDaoHibernate.search(152) | entering 'search' method...
DEBUG [btpool0-1] GenericDaoHibernate.loadCriteria(162) | CriteriaImpl(com.pascalmetrics.model.products.walkrounds.Issue:this[][example (com.pascalmetrics.model.products.walkrounds.Issue@6ee035[created=<null>,updated=<null>,id=<null>,updatingUserId=<null>,comments=<null>,description=<null>,proposedAction=<null>,version=<null>,status=<null>,hidden=false,issueCategory=<null>,subCategory=<null>])])
DEBUG [btpool0-1] GenericDaoHibernate.loadCriteria(190) | adding visit to Critieria.
DEBUG [btpool0-1] GenericDaoHibernate.loadCriteria(162) | Subcriteria(visit:)
DEBUG [btpool0-1] GenericDaoHibernate.loadCriteria(200) | Subcriteria(visit:)
DEBUG [btpool0-1] GenericDaoHibernate.loadCriteria(200) | CriteriaImpl(com.pascalmetrics.model.products.walkrounds.Issue:this[Subcriteria(visit:)][example (com.pascalmetrics.model.products.walkrounds.Issue@6ee035[created=<null>,updated=<null>,id=<null>,updatingUserId=<null>,comments=<null>,description=<null>,proposedAction=<null>,version=<null>,status=<null>,hidden=false,issueCategory=<null>,subCategory=<null>]), example (com.pascalmetrics.model.products.walkrounds.Visit@196a6ac[created=<null>,u
pdated=<null>,id=1,dateOfVisit=<null>,assignment=<null>,numRegisteredNurseAttendees=<null>,updatingUserId=<null>,description=<null>,version=<null>,issues=[],hidden=false,numMedicalDoctorAttendees=100,numOtherAttendees=<null>])])
DEBUG [btpool0-1] SQL.log(401) | select this_.id as id20_2_, this_.created as created20_2_, this_.updated as updated20_2_, this_.updating_user_id as updating4_20_2_, this_.version as version20_2_, this_.hidden as hidden20_2_, this_.category as category20_2_, this_.comments as comments20_2_, this_.description as descript9_20_2_, this_.proposedAction as propose10_20_2_, this_.status as status20_2_, this_.subCategory as subCate12_20_2_, this_.visit_id as visit13_20_2_, visit1_.id as id21_0_, visit1
_.created as created21_0_, visit1_.updated as updated21_0_, visit1_.updating_user_id as updating4_21_0_, visit1_.version as version21_0_, visit1_.hidden as hidden21_0_, visit1_.assignment_id as assignment13_21_0_, visit1_.dateOfVisit as dateOfVi7_21_0_, visit1_.description as descript8_21_0_, visit1_.numMedicalDoctorAttendees as numMedic9_21_0_, visit1_.numOtherAttendees as numOthe10_21_0_, visit1_.numRegisteredNurseAttendees as numRegi11_21_0_, visit1_.visitInterval as visitIn12_21_0_, assignme
nt4_.id as id19_1_, assignment4_.created as created19_1_, assignment4_.updated as updated19_1_, assignment4_.updating_user_id as updating4_19_1_, assignment4_.version as version19_1_, assignment4_.hidden as hidden19_1_, assignment4_.wr_clinical_area_id as wr9_19_1_, assignment4_.executive_id as executive8_19_1_, assignment4_.scheduledVisit as schedule7_19_1_ from wr_issues this_ inner join wr_visit visit1_ on this_.visit_id=visit1_.id left outer join wr_assignment assignment4_ on visit1_.assignm
ent_id=assignment4_.id where (this_.hidden=?) and (visit1_.hidden=? and visit1_.numMedicalDoctorAttendees=?)
Sorry about all the aliases -
THIS IS THE QUESTION:
where (this_.hidden=?) and (visit1_.hidden=? and visit1_.numMedicalDoctorAttendees=?)
why doesn't visit1_id=? appear in this sql????
Thanks