-->
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: Using detached criteria in extended criterion
PostPosted: Thu Jan 05, 2012 2:33 pm 
Newbie

Joined: Thu Jan 05, 2012 2:13 pm
Posts: 2
Hi everyone ,

I have to use math operations using criteria api that generate sql like :

"where (property + (property * 0.2)) > (subselect with count)"

so i start implementing this :

Code:
package br.gov.sp.defensoria.common.hibernate.criterion;

import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.engine.TypedValue;
import org.hibernate.type.StandardBasicTypes;



@SuppressWarnings("serial")
public class OperationExpression implements Criterion {

   
   private final String propertyName1;
   private final String propertyName2;
   private final Object value1;
   private final Object value2;

   public OperationExpression(String propertyName1, String propertyName2,Object value1,Object value2) {
      this.propertyName1 = propertyName1;
      this.propertyName2 = propertyName2;
      this.value1 = value1;
      this.value2 = value2;
   }

   public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
         throws HibernateException {
      
                  //value2 is an instance of DetachedCriteria.

      return " ( " + criteriaQuery.getColumn(criteria, propertyName1) + " + ( " +
      criteriaQuery.getColumn(criteria, propertyName2) +" * "+ value1+ " )) > " +
                 //How can i include detached criteria sql to generate correct sql ??
                  value2);
   }

   public TypedValue[] getTypedValues(Criteria criteria,
         CriteriaQuery criteriaQuery) throws HibernateException {
   return new TypedValue[] {  new TypedValue(StandardBasicTypes.DOUBLE,value1,EntityMode.POJO)};                                                       
   }

   public String toString() {
      return " ( " + propertyName1 + " + ( " propertyName2 +" * "+ value1.toString()+ " )) > " + value2.toString());
   }

}



So when i run the criteria build wrong sql with toString object reference of detached criteria :
Code:
(property + ( property* 0.2 )) > DetachedCriteria@fdsi9fd77 (toString() of detached criteria)


How can i get the sql instead of toString() ???


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.