我试图在hibernate中动态地将列名传递给查询但我无法这样做。你能说清楚如何做到这一点吗?我尝试了Restrictions
,如下所示:
getCurrentSession()
.createCriteria(Result.class)
.add(Restrictions.eq(option.column_name, "first_test")).list();
例外:
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)
答案 0 :(得分:0)
Result
似乎没有财产
test_name,le
请确保传递正确的字段名称,例如:
@Entity
class Employee{
..
private String firstName;
private String lastName;
..
}
如果用户选择firstName
,您只需将字段名称作为String
传递:
String fieldName = "firstName";
getCurrentSession().createCriteria(Employee.class)
.add(Restrictions.eq(fieldName, "John")).list();