0

I have used hibernate reverse engineering to generate my model and Dao classes i am using hibernate criteria API to retrive values from database

    criteria.add(Restrictions.eq(
            "propert1.propert2.StateId", 1));

i am getting the following exception

javax.faces.el.EvaluationException: org.hibernate.QueryException: could not resolve property: propert1.propert2.StateId of: com.packagename.

Has any body got the clue why is this happening.(create criteria has the class which has property1 and class of propert1 have propert2 )

Niks
  • 4,802
  • 4
  • 36
  • 55
curious
  • 915
  • 4
  • 14
  • 27
  • Capital "S"? By convention Java bean property names begin with a small letter (i.e. `getName` => Property name is `name`). – Hauke Ingmar Schmidt Feb 22 '12 at 10:45
  • yes i have changed it here, but in my class these are properly named because these classes are generated through hibernate reverse engineering, so that is not the problem – curious Feb 22 '12 at 11:03

2 Answers2

1

Defining aliases should do it

criteria.createAlias("propert1","pr1")
    .createAlias("pr1.propert2","pr2")
    .add(Restrictions.eq("pr2.StateId", 1));
Niks
  • 4,802
  • 4
  • 36
  • 55
  • thanks for ur reply i have tried something like this criteria = criteria.createCriteria("propert1").createCriteria("propert2").add(Restrictions.eq("stateId", 1)); and it worked for me. i will try it ur way also. – curious Feb 23 '12 at 07:23
  • @curious : Both approaches are almost identical. Refer to http://stackoverflow.com/questions/2347359/hibernate-createcriteria-or-createalias and alike, if you are really "curious" ;) – Niks Feb 23 '12 at 10:23
  • @curious : Also, would help someone if you update your question with your solution! – Niks Feb 23 '12 at 10:32
  • i have tried ur way but i got the following exception org.hibernate.QueryException: could not resolve property: stateId of: Myclass – curious Feb 24 '12 at 17:13
0

This Worked for me

Criteria criteria = session
            .createCriteria(MyClass.class);
criteria = criteria.createCriteria("propert1").createCriteria("propert2").add(Restrictions.eq("stateId", 1));
curious
  • 915
  • 4
  • 14
  • 27