Hibernate列名参数绑定
我在HQL语句中绑定命名参数,但它没有被填充Hibernate列名参数绑定,hibernate,Hibernate,我在HQL语句中绑定命名参数,但它没有被填充 //colname = "AdminsInfo.name"; assume it is from method's input //colval = input.getName().toString(); // assume it is from method's input String query = "from AdminsInfo where :coln = :colv"; Query q = session.createQuery(que
//colname = "AdminsInfo.name"; assume it is from method's input
//colval = input.getName().toString(); // assume it is from method's input
String query = "from AdminsInfo where :coln = :colv";
Query q = session.createQuery(query);
q.setParameter("coln",colname);
q.setParameter("colv",colval);
System.out.println(q.toString());
它输出类似这样的内容,这意味着未设置参数(coln,colv),并返回0条记录
QueryImpl(from AdminsInfo where :coln = :colv)
Hibernate: select adminsinfo0_.Row as Row1_0_, adminsinfo0_.ID as ID2_0_,adminsinfo0_.Name as Name3_0_, ... where ?=?
欢迎提供有关如何在HQL语句中绑定列名的任何帮助。谢谢Mahdi。不能将列名绑定为参数。只有一个列值。在绑定参数值和执行查询之前,必须在计算执行计划时知道此名称。如果您真的希望有这样一个动态查询,请使用Criteria API或其他动态创建查询的方法。尝试替换Criteria的查询
Criteria c = session.createCriteria(AdminsInfo.class);
c.add(Restrictions.eq(colname,colval));
c.list();