Hibernate从通用游标获取输出 [英] Hibernate Getting Output from a Generic Cursor
问题描述
需要使用复杂的过程从oracle数据库中获取数据.输出是通用游标.我正在使用休眠来获取输出.但是结果集没有映射,因为它不是某些表.我该如何从游标中获取数据到我的对象中.
Need to get data out from oracle database using a complex procedure. The output is a generic cursor. i am using hibernate to fetch the output. But the Result set does not have a mapping as it is not some table. how can i fetch the data out from the cursor into my object.
有没有一种方法可以让我在下面获得结果集对象而不是列表?
我可以从Session工厂获得连接并使用可调用语句.那是好习惯吗?
Is there a way i can get a resultset object below instead of a list ?
I can get a connection from Session factory and use callable statement. is that good practice ?
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提供了一个ScrollableResults
来处理游标.这是 API文档.
Hibernate provides a ScrollableResults
to deal with the cursor. Here is an example from the 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();
此处是另一个示例.
这篇关于Hibernate从通用游标获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!