Hibernate criteria for compare SUM of three columns againts another column -
i have table columns id, col1, col2, col3, col4, col5. want add hibernate criteria restriction (col1 + col2 + col3) < col4
all these columns contains integer values. how achieve requirement ? appreciated.
i found can achieving writing customized criterion. following sample code share others,
public class costomizedexpression implements criterion { private final typedvalue[] no_typed_values = new typedvalue[0]; private string[] othercolumns; private string column; private string operator; public costomizedexpression (string operator, string column, string... othercolumns) { this.column = column; this.othercolumns = othercolumns; this.operator = operator; } @override public string tosqlstring(criteria criteria, criteriaquery criteriaquery) throws hibernateexception { string[] columns = criteriaquery.getcolumnsusingprojection(criteria, column); stringbuffer buffer = new stringbuffer(); buffer.append("("); (int = 0; < othercolumns.length; i++) { buffer.append(" "); buffer.append(criteriaquery.getcolumnsusingprojection(criteria, othercolumns[i])[0]); if (i < othercolumns.length - 1) { buffer.append("+"); } buffer.append(" "); } buffer.append(" )"); buffer.append(operator); buffer.append(" "); buffer.append(columns[0]); return buffer.tostring(); } @override public typedvalue[] gettypedvalues(criteria criteria, criteriaquery criteriaquery) throws hibernateexception { return no_typed_values; } @override public string tostring() { stringbuffer buffer = new stringbuffer(); (int = 0; < othercolumns.length; i++) { buffer.append(" "); buffer.append(othercolumns[i]); if (i < othercolumns.length - 1) { buffer.append("+"); } buffer.append(" "); } buffer.append(" "); buffer.append(operator); buffer.append(" "); buffer.append(column); return buffer.tostring(); } }
Comments
Post a Comment