-->
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: Trouble building a dynamic query using criterias and joins
PostPosted: Tue Jun 03, 2008 4:11 pm 
Beginner
Beginner

Joined: Sat Jul 08, 2006 2:58 pm
Posts: 26
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.


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.