如果有多列IN子句,如何为hibernate SQL查询设置参数?
例如,SQL查询应该如下生成:
SELECT * FROM TABLE TA WHERE (TA.COLUMN1 , TA.COLUMN2) IN ((C1v, C2v), (C1v, C2v), .....);
我看到这可以通过Spring jdbcTemplate来实现here,但我想在Hibernate SQL Query中使用它。
我尝试了以下方案,但它们都不适合我:
String sql = "SELECT * FROM TABLE TA WHERE (TA.COLUMN1 , TA.COLUMN2) IN (:ids)";
// 1. List of BigDecimal array's
List<BigDecimal[]> ids = new ArrayList<>();
BigDecimal[] bd1 = {new BigDecimal(100119401), new BigDecimal(2)};
BigDecimal[] bd2 = {new BigDecimal(100119000), new BigDecimal(1)};
ids.add(bd1);
ids.add(bd2);
Query query = session.createSQLQuery(sql);
query.setParameterList("ids", ids);
// 2. List of Object arrays
List<Object []> ids = new ArrayList<>();
Object [] bd1 = {new BigDecimal(100119401), new BigDecimal(2)};
Object [] bd2 = {new BigDecimal(100119000), new BigDecimal(1)};
ids.add(bd1);
ids.add(bd2);
Query query = session.createSQLQuery(sql);
query.setParameterList("ids", ids);
// 3. List of inner lists with ids
List<BigDecimal> ob1 = new ArrayList<BigDecimal>();
ob1.add(new BigDecimal(100119401));ob1.add(new BigDecimal(2));
List<BigDecimal> ob2 = new ArrayList<BigDecimal>();
ob2.add(new BigDecimal(100119401));ob2.add(new BigDecimal(2));
List<List<BigDecimal>> ids = new ArrayList<List<BigDecimal>>();
ids.add(ob1);ids.add(ob2);
Query query = session.createSQLQuery(sql);
query.setParameterList("ids", ids);
在所有情况下,我得到“ORA-00920:无效的关系运营商”错误。
例外:
Caused by: java.sql.SQLSyntaxErrorException: ORA-00920: invalid relational operator
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:776)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:897)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1034)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3867)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1502)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
... 141 more
答案 0 :(得分:0)
这样的事情应该有效:
String sql = "SELECT * FROM TABLE TA WHERE CONCAT(TA.COLUMN1, '|', TA.COLUMN2) IN (:values)";
List<String> values = new ArrayList<>();
values.add("100119401|2");
values.add("100119000|1");
// ...
Query query = session.createSQLQuery(sql);
query.setParameterList("values", values);
NB:确保使用的分隔符|
不在您的数据中