0

I need to prepare a query some thing like below dynamically, I am getting group column in a different list and main column in a different list, Can some one help me?

    SELECT * FROM TABLE WHERE COLUMN_A =1 ? OR (COLUMN_B='A' AND COLUMN_B='Z')

Java code:

    CriteriaBuilder builder;
    List<Predicate> predicates = new ArrayList<Predicate>();
    List<Predicate> innerPredicates = new ArrayList<Predicate>();
    List<Predicate> outerPredicates = new ArrayList<Predicate>();

    for(GroupClause gc : list.getGroupClause()){
        List<Predicate> groupPredicates = new ArrayList<Predicate>();
        groupPredicates.add(gc.getColumnB());

        if(groupPredicates!=null && groupPredicates.size()>0){
            innerPredicates.add(builder.and(groupPredicates.toArray(new Predicate[groupPredicates.size()])));
        }
    }

    if(outerPredicates.size()>0){
        predicates.add(builder.or(outerPredicates.toArray(new Predicate[outerPredicates.size()])));
        predicates.add(builder.or(innerPredicates.toArray(new Predicate[innerPredicates.size()])));
    }

The put for above code is

    SELECT * FROM TABLE WHERE COLUMN_A =1 ? AND COLUMN_B='A' AND COLUMN_B='Z'
perissf
  • 15,979
  • 14
  • 80
  • 117
user3157090
  • 517
  • 3
  • 12
  • 29
  • I really don't understand what your code is trying to achieve. I don't see any code that compares a column with the value 1 or the values 'A' and 'Z'. I see you trying to create a "group by" and then also "and" the group-by columns together (which is not meaningful). Also don't understand what exactly goes wrong according to yourself. – Erwin Bolwidt Jul 28 '14 at 06:26

1 Answers1

0

Use simple Criteria here is the example

public List<TABLE> getTABLE () {

        Criteria tableCriteria= session.createCriteria(TABLE.class);
        tableCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
         tableCriteria.add(Restrictions.and(Restrictions.eq("COLUMN_A",1),Restrictions.or(Restrictions.eq("COLUMN_B","A"),
               Restrictions.eq("COLUMN_B","Z")))


        return tableCriteria.list();
    }
Ramzan Zafar
  • 1,562
  • 2
  • 17
  • 18