0

what is difference between criteria.add and criteria.equals in hiberante criteria?

just example:

returns 25 records

criteria.equals(Restrictions.eq("templateType", TemplateType.DEFAULT_TEMPLATE));

returns 2 records:

criteria.add(Restrictions.eq("templateType", TemplateType.DEFAULT_TEMPLATE)); 

when I do hibernate.show_sql, then output is

  • for criteria.equals, no syntax generated
  • for criteria.add , this_.template_type=?

Note: Using Hibernate 3.

Edit:

try{
List<Form> forms=Collections.emptyList();
Criteria normCriteria=session.createCriteria(NormMaster.class);
normCriteria.add(Restrictions.eq("id",normId));
normCriteria.setProjection(Projections.property("libraryId"));
List<Long> libNormIds=normCriteria.list();

if(libNormIds!=null && libNormIds.size()>0)
{
    Criteria criteria=session.createCriteria(Form.class);
    criteria.add(Restrictions.in("normId", libNormIds));
    criteria.equals(Restrictions.eq("templateType", TemplateType.DEFAULT_TEMPLATE));
    //criteria.add(Restrictions.eq("templateType", TemplateType.DEFAULT_TEMPLATE));
    criteria.setFetchMode("formControlMaps", FetchMode.JOIN);
    criteria.setFetchMode("formControlMaps.data", FetchMode.JOIN);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    forms=criteria.list();
}
return forms;
}
 catch (Exception e) {
     e.printStackTrace();
}
bNd
  • 7,512
  • 7
  • 39
  • 72

3 Answers3

1

There is nothing to compare criteria.add with criteria.equals.

criteria.add adds Criterion to your Criteria, like your case bellow:

 criteria.add(Restrictions.eq("templateType", TemplateType.DEFAULT_TEMPLATE)); 

Here Restrictions.eq("templateType", TemplateType.DEFAULT_TEMPLATE) returns a Criterion object, that is added in your Criteria object.

criteria.equals is an inherited method from the Object class and used for checking equality of java objects! So your code bellow,

 criteria.equals(Restrictions.eq("templateType", TemplateType.DEFAULT_TEMPLATE));

will do nothing to your Criteria object, but only returns false. You can be over sure of my statement by simply commenting out the criteria.equals code portion.

You will see that this line has no effect in generating your query!

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
0

criteria.equals: is actually the object equals method which checks whether the passed object is equal to the LHS

criteria.add: Adds the passed parameter to the criterias where clause

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Adding More to Sazzadur's answer. Criteria interface is implemented by CriteriaImpl and other classes that extends Object class. So criteria.equals invoke Object class's equals method. Ad this is not part of query generation so you not see anything in query generated by hibernate for your criteria.

Hope this helps.

Harish Kumar
  • 528
  • 2
  • 15