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() ???