HQL:可变列 [英] HQL: variable column
本文介绍了HQL:可变列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Query criteria = session.createQuery(
select test.col1,test.col2,test.col3
from Test test+
where test.col =:variableValue);
criteria.setInteger(variableValue ,10);
但是可以像这样设置变量列吗?
字符串variableColumn =test.col1;
Query criteria = session.createQuery(
select test.col1,test .col2,test.col3
from Test test+
其中:variableColumn =:variableValue);
criteria.setInteger(variableValue,10);
criteria.setString(variableColumn,variableColumn);
这是结果:
线程main中的异常Hibernate:select .... where?=? ...
org.hibernate.exception.SQLGrammarException:无法在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
... $ b $执行查询
b在_test.TestCriteria.main(TestCriteria.java:44)
导致:com.microsoft.sqlserver.jdbc.SQLServerException:将nvarchar值'test.col1'转换为数据类型int时转换失败。
...
更新(工作解决方案):
查询条件= session.createQuery(
select test.col1,test.col2,test.col3 $ b $
其中(:value1为null或test.col1 =:value1)AND
(:value2为null或test.col2 =:value2)
解决方案
这在您的应用程序中有意义吗?
String test =select test.col1,test.col2,test.col3+
from Test test+
where {columnName} =:variableValue;
Object variableValue = //从某处获取
String columnName = //另一个从某处获取
query = query.replace({columnName},columName );
//现在继续照常
这通常是一个 naive 查询构造函数您可能需要将此想法重构为一个单独的基于实用程序/实体的类来完善(例如S QL注入)执行前的查询。
I'm able to set variable values for "where" restrictives:
Query criteria = session.createQuery(
"select test.col1, test.col2, test.col3
"from Test test " +
"where test.col = :variableValue ");
criteria.setInteger("variableValue", 10);
But is it possible to set variable column like this?
String variableColumn = "test.col1";
Query criteria = session.createQuery(
"select test.col1, test.col2, test.col3
"from Test test " +
"where :variableColumn = :variableValue ");
criteria.setInteger("variableValue", 10);
criteria.setString("variableColumn", variableColumn);
This is the result:
Exception in thread "main" Hibernate: select .... where ?=? ...
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
...
at _test.TestCriteria.main(TestCriteria.java:44)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the nvarchar value 'test.col1' to data type int.
...
UPDATE (working solution):
Query criteria = session.createQuery(
"select test.col1, test.col2, test.col3
"from Test test " +
"where (:value1 is null OR test.col1 = :value1) AND
(:value2 is null OR test.col2 = :value2) "
解决方案
Does this make sense in your application:
String query = "select test.col1, test.col2, test.col3" +
"from Test test " +
"where {columnName} = :variableValue ";
Object variableValue = // retrieve from somewhere
String columnName = // another retrieve from somewhere
query = query.replace("{columnName}", columName);
// Now continue as always
This is generally a naive query constructor. You may need to refactor this idea to a separate utility/entity-based class to refine (e.g. SQL injection) the queries before execution.
这篇关于HQL:可变列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文