0

I am trying to pass a column name dynamically to the query in hibernate but I have not been able to do so. Can you shed light on how to do this? I have tried Restrictions like the following:

getCurrentSession()
.createCriteria(Result.class)
.add(Restrictions.eq(option.column_name, "first_test")).list();

Exception:

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.hibernate.QueryException: could not resolve property:
"test_name,le" of: com..model.Result
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
user1647708
  • 447
  • 3
  • 9
  • 22
  • When you say pass a "column name" dynamically, do you mean "property name"? Or do you really mean that you are trying to dynamically choose how a given property of the object is mapped to a column in the database? – Alex Godofsky Sep 05 '13 at 20:32
  • I meant property name. For example I have a table with following columns: column1, column2. These are displayed as select options in the UI and I want to use them to create query like "select * from table where column1=some value". If column2 is selected from the dropdown the query will be "select * from table where column2=some value" – user1647708 Sep 05 '13 at 20:50
  • I think the answer, then, is just that you fed it a string that isn't a property name. It is hard to image that "test_name,le" is the name of one of your properties. – Alex Godofsky Sep 05 '13 at 20:52

1 Answers1

0
  • It seems like your entity Result doesn't have property test_name,le

Please make sure you pass correct field name, for example:

@Entity
class Employee{
   ..
   private String firstName;
   private String lastName;
   ..
} 

If user selected firstName you just need to pass the name of the field as String:

String fieldName = "firstName";
getCurrentSession().createCriteria(Employee.class)
.add(Restrictions.eq(fieldName, "John")).list();
Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
  • When I do following I do get results as expected "return getCurrentSession().createCriteria(TestResult.class).add(Restrictions.eq("test_name", "first_test")).list();" But when I do "return getCurrentSession().createCriteria(TestResult.class).add(Restrictions.eq(option.getName(), "first_test")).list();" I get the error as above. Do I need to map the strings to the property somewhere? – user1647708 Sep 05 '13 at 21:00
  • Are you sure that `option.‌​getName()` returns correct field name? – Sergii Shevchyk Sep 05 '13 at 21:02
  • you are correct. the getoption was returning incorrect response because I had two options with same name which resulted in the conflict. – user1647708 Sep 05 '13 at 21:56