gpt4 book ai didi

java - 如何使用hibernate滚动ScrollableResults进行批处理?

转载 作者:行者123 更新时间:2023-11-30 07:21:06 24 4
gpt4 key购买 nike

假设您已经配置了 hibernate 批处理大小以支持批处理操作。

    blPU.hibernate.jdbc.batch_size=1000

那么如何编写代码块来处理批处理?

最佳答案

    int BATCH_SIZE = 1000;
DynamicDaoHelper dynamicDaoHelper = new DynamicDaoHelperImpl();
SessionFactory sessionFactory = dynamicDaoHelper.getSessionFactory((HibernateEntityManager) em);
Session session = sessionFactory.openSession();
Transaction tx = session.openTransaction();
// must use HQL query so that scroll.get(0) returns entity.
// if use native SQL, scroll.get() returns row object.
org.hibernate.Query q = session.createQuery(" SELECT p FROM Product p WHERE p.isProcessed = false");
ScrollableResults scroll = q.setCacheable(false).setFetchSize(100).scroll(ScrollMode.FORWARD_ONLY);
boolean hasNext = scroll.next();
try {
List<Product> batchProduct = new ArrayList<Product>(BATCH_SIZE);
while (hasNext) {
batchProduct.clear();
int count = 0;

// get a batch of products, quantity should be same as BATCH_SIZE
while (hasNext) {
Product product = (Product) scroll.get(0);
batchProduct.add(product);
count++;
hasNext = scroll.next();
if (count % BATCH_SIZE == 0 || !hasNext)
break;
}

// process batch
processBatch(batchProduct);

// persist batch and release memory
session.flush();
session.clear();
}
} finally {
scroll.close();
}
tx.commit();
session.close();

注意:调用scroll.next()将导致检查是否存在并移至下一条记录,应使用 boolean 标志仅调用next()一次。

关于java - 如何使用hibernate滚动ScrollableResults进行批处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37562225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com