【发布时间】:2011-04-28 01:41:41
【问题描述】:
Oracle 支持将表连接到 Oracle 集合类型(嵌套表或 VARRAY)的查询语法。可以使用此语义代替 SQL 中的 in (1,2,3) 语法,并允许将值数组绑定到查询。这可以使用 Oracle 的 JDBC 驱动程序来完成。
这种类型的查询称为 Pickler Fetch。它比使用 SQL IN 列表更具可扩展性。我的应用程序的集合中可能有大约 10,000 个值。
我的问题是我是 Hibernate 的新手(我们使用的是 Hibernate 3.2.5 和 Spring 2.0.6)并且看不到如何使用 Hibernate 实现这种语义。 通常 JDBC 实现会像这样工作: 使用 SQL*Plus 中的 CREATE 类型在数据库中定义用户定义类型 创建或替换类型 NUMBER_LIST_TYPE 作为数字表;
在 Java 中:
import java.sql.*;
import oracle.sql.ArrayDescriptor;
import oracle.sql.ARRAY;
import oracle.jdbc.*;
/* The oracle collection is described */
ArrayDescriptor oracleCollection =
ArrayDescriptor.createDescriptor("NUMBER_LIST_TYPE",conn);
PreparedStatement stmt = conn.prepareStatement(
" SELECT ename,empno FROM emp "
+" WHERE empno IN ( "
+" SELECT * FROM TABLE( CAST ( ? as NUMBER_LIST_TYPE ) ) "
+" ) "
);
/* define our java array */
int[] javaArray1 = { 7369,7566,7782 };
/* define our oracle array */
ARRAY jdbcArray1 = new ARRAY (oracleCollection, conn, javaArray1);
/* bind that array to our statement bind variable */
stmt.setObject(1,jdbcArray1);
/* execute the query and browse the result */
ResultSet r=stmt.executeQuery();
while(r.next()){
System.out.println(
"\t"+"\t"+r.getString(2)+": "+r.getString(1));
}
现在如何使用 Hibernate 实现类似的功能?
【问题讨论】: