簡體   English   中英

休眠-使用ScrollMode.FORWARD_ONLY獲取ScrollableResults的最后一個元素

[英]Hibernate - Get the last element of a ScrollableResults with ScrollMode.FORWARD_ONLY

簡單的Java批處理使用JPA / Hibernate讀取數百萬行。 我的問題是:如何使用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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM