gpt4 book ai didi

java - Hibernate - 使用 ScrollMode.FORWARD_ONLY 获取 ScrollableResults 的最后一个元素

转载 作者:行者123 更新时间:2023-12-01 09:21:15 24 4
gpt4 key购买 nike

使用 JPA/Hibernate 的简单 Java 批量读取数百万行。我的问题是:如何使用 ScrollMode.FORWARD_ONLY 识别 ScrollableResults 中的最后结果?

使用 isLast() 方法,它不是仅向前滚动模式的选项,并且使用 ScrollMode.SCROLL_INSENSITIVE 会降低我的批处理的性能。

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.StatelessSession;

StatelessSession statelessSession = ((Session) em.getDelegate()).getSessionFactory().openStatelessSession();
Query query = statelessSession.createSQLQuery("MY_QUERY")
.setReadOnly(true)
.setFetchSize(Integer.valueOf(1000));
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
while (results.next()) {
...
//I need to extract a timestamp from the last record here
}

最佳答案

您是否考虑过这种“基本”方法(如果是这样,为什么它不起作用?):

ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);

Timestamp ts = null;
while (results.next()) {
...
//I need to extact a timestamp from the last record here
ts = results.getTimestamp("timestamp_column");
}

// you'll have here ts with the value of the latest row.
// Be careful, it can be null in case of empty result set.

关于java - Hibernate - 使用 ScrollMode.FORWARD_ONLY 获取 ScrollableResults 的最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40152908/

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