假设我们有 HQL 查询:
String ageQuery = "select age from people where name = :name";
Session session = getSession();
Query query = session.
createSQLQuery(nameQuery).
setParameter("name", "Thomas");
// query.list(); returns the result
但是如果我想参数化表名怎么办?不能这样使用它:
String ageQuery = "select :table from people where name = :name";
Session session = getSession();
Query query = session.
createSQLQuery(ageQuery).
setParameter("table", "age").
setParameter("name", "Thomas");
我已经应用了解决方法:
String ageQuery = "select :table: from people where name = :name";
Session session = getSession();
Query query = session.
createSQLQuery(ageQuery.
replace(":table:", "age")).
setParameter("name", "Thomas");
......但我真的不喜欢它。还有其他解决方案吗?