I am trying to learn how to use criterias to build q dynamic query. Basically I am trying to figure out how to build the following query dynamically:
Code:
"from Person p inner join p.addresses address inner join address.addressLines lines " +
"where p.firstName = :firstName and " +
"p.lastName = :lastName and " +
"address.city = :city and " +
"address.state = :state and " +
"address.zip = :zip and " +
"lines = :address");
Here is the code that I am using to do this:
Code:
public List<Person> getRecordsBySearchCriteria(final PersonSearchDTO dto){
HibernateTemplate ht = new HibernateTemplate(this.sessionFactory);
return (List<Person>) ht.execute(new HibernateCallback(){
public Object doInHibernate(org.hibernate.Session session){
Criteria personCriteria = session.createCriteria(Person.class);
if(StringUtils.isNotBlank(dto.getFirstName())){
personCriteria.add(Expression.eq("firstName", dto.getFirstName()));
}
if(StringUtils.isNotBlank(dto.getLastName())){
personCriteria.add(Expression.eq("lastName", dto.getLastName()));
}
if((StringUtils.isNotBlank(dto.getHomeCity())) ||
(StringUtils.isNotBlank(dto.getHomeState())) ||
(StringUtils.isNotBlank(dto.getHomeZip())) ||
(StringUtils.isNotBlank(dto.getHomeAddress()))){
Criteria addressCriteria = personCriteria.createAlias("addresses", "address");
if(StringUtils.isNotBlank(dto.getHomeCity())){
addressCriteria.add(Expression.eq("adddress.city", dto.getHomeCity()));
}
if(StringUtils.isNotBlank(dto.getHomeState())){
addressCriteria.add(Expression.eq("address.state", dto.getHomeState()));
}
if(StringUtils.isNotBlank(dto.getHomeZip())){
addressCriteria.add(Expression.eq("address.zip", dto.getHomeZip()));
}
if(StringUtils.isNotBlank(dto.getHomeAddress())){
addressCriteria.createAlias("addressLines", "lines")
.add(Expression.eq("lines", dto.getHomeAddress()));
}
return addressCriteria.list();
}
personCriteria.setMaxResults(6);
return personCriteria.list();
}
});
}
When I run my unit test I get the following errors:
[junit] Testcase: testGetPersonrecordsBySearchCriteria(com.chisq.person.facade.PersonSearchFacadeTest): Caused an ERROR
[junit] could not resolve property: adddress of: com.chisq.common.bo.Person; nested exception is org.hibernate.QueryException: could not resolve propert
y: adddress of: com.chisq.common.bo.Person
[junit] org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: adddress of: com.chisq.common.bo.Person; nested exceptio
n is org.hibernate.QueryException: could not resolve property: adddress of: com.chisq.common.bo.Person
[junit] org.hibernate.QueryException: could not resolve property: adddress of: com.chisq.common.bo.Person
[junit] at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
[junit] at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
[junit] at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)
[junit] at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
[junit] at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
[junit] at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:433)
[junit] at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:393)
[junit] at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
[junit] at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:333)
[junit] at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
[junit] at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
[junit] at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1514)
[junit] at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
[junit] at com.chisq.person.dao.PersonHibernateDAO$1.doInHibernate(PersonHibernateDAO.java:104)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:334)
[junit] at com.chisq.person.dao.PersonHibernateDAO.getRecordsBySearchCriteria(PersonHibernateDAO.java:68)
[junit] at com.chisq.person.facade.PersonFacadeImpl.getRecordsBySearchCriteria(PersonFacadeImpl.java:190)
[junit] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
[junit] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
[junit] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
[junit] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
[junit] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
[junit] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
[junit] at $Proxy4.getRecordsBySearchCriteria(Unknown Source)
[junit] at com.chisq.person.facade.PersonSearchFacadeTest.testGetPersonrecordsBySearchCriteria(PersonSearchFacadeTest.java:98)
My question is how to add a join to my query dynamically? What am I doing wrong?? Thanks for any help anyone can give.