Search code examples
hibernatecriteria

Need help to create hibernate criteria


I am new to hibernate criteria.please correct me if i am wrong in below code.

        Criteria c = getSessionFactory().getCurrentSession().createCriteria(JdwCustomerTlrdRef.class);
        c.add(Restrictions.ilike("operations_spec", "%AAL%"));

        List<JdwCustomerTlrdRef> customerTlrdRefSysId = c.list();

I applied restrictions to operations_spec it will returns all AAL customer name.


Solution

  • Try this.

    Criteria c = getSessionFactory().getCurrentSession().createCriteria(JdwCustomerTlrdRef.class); 
    

    if you want to find exact "AAL" then

    c.add(Restrictions.eq("operations_spec", "AAL" )); 
    

    OR if you want all the customer who has AAL in anywhere in operations_spec field i.e "abAALcd" then.

    c.add(Restrictions.like("operations_spec", "AAL" , MatchMode.ANYWHERE)); 
    

    There is one more thing, you are using field name as "operations_spec", it looks like field name in db. if you want to use in criteria then you should use the property name which I think would be operationSpecs if you are following java member name coding practice.

    if this is the case then apply restriction like this.

    c.add(Restrictions.eq("operationSpecs", "AAL" ));