【问题标题】:Hibernate Getting Output from a Generic CursorHibernate 从通用游标获取输出
【发布时间】:2012-03-27 05:54:04
【问题描述】:

需要使用复杂的过程从 oracle 数据库中获取数据。输出是一个通用游标。我正在使用休眠来获取输出。但是结果集没有映射,因为它不是某个表。如何将光标中的数据提取到我的对象中。

有没有办法我可以在下面获取结果集对象而不是列表?
我可以从会话工厂获得连接并使用可调用语句。这是好习惯吗?

Query query = session.createSQLQuery(
    "CALL GetStocks(:stockCode)")
    .addEntity(Stock.class)
    .setParameter("stockCode", "7277");

List result = query.list();
for(int i=0; i<result.size(); i++){
    Stock stock = (Stock)result.get(i);
    System.out.println(stock.getStockCode());

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    Hibernate 提供了一个ScrollableResults 来处理光标。这是来自API docs 的示例。

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    
    ScrollableResults customers = session.getNamedQuery("GetCustomers")
        .setCacheMode(CacheMode.IGNORE)
        .scroll(ScrollMode.FORWARD_ONLY);
    int count=0;
    while ( customers.next() ) {
        Customer customer = (Customer) customers.get(0);
        customer.updateStuff(...);
        if ( ++count % 20 == 0 ) {
            //flush a batch of updates and release memory:
            session.flush();
            session.clear();
        }
    }
    
    tx.commit();
    session.close();
    

    Here 是另一个例子。

    【讨论】:

    • 为此,您必须使用一些表名称来定义客户映射对象,以告知哪一列映射到哪一列......但我的结果集是多个表的组合作为通用游标.. . 客户 customer = (Customer) customers.get(0);
    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 2014-06-18
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多