@Override protected void doClose() throws Exception { helper.close(); super.doClose(); }
/** * The Hibernate SessionFactory to use the create a session. * * @param sessionFactory the {@link SessionFactory} to set */ public void setSessionFactory(SessionFactory sessionFactory) { helper.setSessionFactory(sessionFactory); }
@Override public void afterPropertiesSet() throws Exception { Assert.state(fetchSize >= 0, "fetchSize must not be negative"); helper.afterPropertiesSet(); }
@Test public void testSessionReset() throws Exception { StatelessSession session = mock(StatelessSession.class); when(sessionFactory.openStatelessSession()).thenReturn(session); helper.setSessionFactory(sessionFactory); helper.createQuery(); assertNotNull(ReflectionTestUtils.getField(helper, "statelessSession")); helper.close(); assertNull(ReflectionTestUtils.getField(helper, "statelessSession")); }
@Test public void testOneSessionForAllPages() throws Exception { StatelessSession session = mock(StatelessSession.class); when(sessionFactory.openStatelessSession()).thenReturn(session); helper.setSessionFactory(sessionFactory); helper.createQuery(); // Multiple calls to createQuery only creates one session helper.createQuery(); }
/** * Read a page of data, clearing the existing session (if necessary) first, * and creating a new session before executing the query. * * @param page the page to read (starting at 0) * @param pageSize the size of the page or maximum number of items to read * @param fetchSize the fetch size to use * @param parameterValues the parameter values to use (if any, otherwise * null) * @return a collection of items */ public Collection<? extends T> readPage(int page, int pageSize, int fetchSize, Map<String, Object> parameterValues) { clear(); Query query = createQuery(); if (parameterValues != null) { query.setProperties(parameterValues); } @SuppressWarnings("unchecked") List<T> result = query.setFetchSize(fetchSize).setFirstResult(page * pageSize).setMaxResults(pageSize).list(); return result; }
/** * Get a cursor over all of the results, with the forward-only flag set. * * @param fetchSize the fetch size to use retrieving the results * @param parameterValues the parameter values to use (or null if none). * * @return a forward-only {@link ScrollableResults} */ public ScrollableResults getForwardOnlyCursor(int fetchSize, Map<String, Object> parameterValues) { Query query = createQuery(); if (parameterValues != null) { query.setProperties(parameterValues); } return query.setFetchSize(fetchSize).scroll(ScrollMode.FORWARD_ONLY); }
/** * A query provider. Either this or the {{@link #setQueryString(String) * query string} or the {{@link #setQueryName(String) query name} should be * set. * * @param queryProvider Hibernate query provider */ public void setQueryProvider(HibernateQueryProvider<? extends T> queryProvider) { helper.setQueryProvider(queryProvider); }
/** * A query name for an externalized query. Either this or the { * {@link #setQueryString(String) query string} or the { * {@link #setQueryProvider(HibernateQueryProvider) query provider} should * be set. * * @param queryName name of a hibernate named query */ public void setQueryName(String queryName) { helper.setQueryName(queryName); }
/** * Update the context and clear the session if stateful. * * @param executionContext the current {@link ExecutionContext} * @throws ItemStreamException if there is a problem */ @Override public void update(ExecutionContext executionContext) throws ItemStreamException { super.update(executionContext); helper.clear(); }
/** * Open hibernate session and create a forward-only cursor for the query. */ @Override protected void doOpen() throws Exception { Assert.state(!initialized, "Cannot open an already opened ItemReader, call close first"); cursor = helper.getForwardOnlyCursor(fetchSize, parameterValues); initialized = true; }
@Override protected void doReadPage() { if (results == null) { results = new CopyOnWriteArrayList<>(); } else { results.clear(); } results.addAll(helper.readPage(getPage(), getPageSize(), fetchSize, parameterValues)); }
/** * Wind forward through the result set to the item requested. Also clears * the session every now and then (if stateful) to avoid memory problems. * The frequency of session clearing is the larger of the fetch size (if * set) and 100. * * @param itemIndex the first item to read * @throws Exception if there is a problem * @see AbstractItemCountingItemStreamItemReader#jumpToItem(int) */ @Override protected void jumpToItem(int itemIndex) throws Exception { int flushSize = Math.max(fetchSize, 100); helper.jumpToItem(cursor, itemIndex, flushSize); }
/** * Read a page of data, clearing the existing session (if necessary) first, * and creating a new session before executing the query. * * @param page the page to read (starting at 0) * @param pageSize the size of the page or maximum number of items to read * @param fetchSize the fetch size to use * @param parameterValues the parameter values to use (if any, otherwise * null) * @return a collection of items */ public Collection<? extends T> readPage(int page, int pageSize, int fetchSize, Map<String, Object> parameterValues) { clear(); Query query = createQuery(); if (parameterValues != null) { query.setProperties(parameterValues); } @SuppressWarnings("unchecked") List<T> result = query.setFetchSize(fetchSize).setFirstResult(page * pageSize).setMaxResults(pageSize).list(); return result; }
/** * Get a cursor over all of the results, with the forward-only flag set. * * @param fetchSize the fetch size to use retrieving the results * @param parameterValues the parameter values to use (or null if none). * * @return a forward-only {@link ScrollableResults} */ public ScrollableResults getForwardOnlyCursor(int fetchSize, Map<String, Object> parameterValues) { Query query = createQuery(); if (parameterValues != null) { query.setProperties(parameterValues); } return query.setFetchSize(fetchSize).scroll(ScrollMode.FORWARD_ONLY); }
/** * A query provider. Either this or the {{@link #setQueryString(String) * query string} or the {{@link #setQueryName(String) query name} should be * set. * * @param queryProvider Hibernate query provider */ public void setQueryProvider(HibernateQueryProvider<T> queryProvider) { helper.setQueryProvider(queryProvider); }
/** * A query name for an externalized query. Either this or the { * {@link #setQueryString(String) query string} or the { * {@link #setQueryProvider(HibernateQueryProvider) query provider} should * be set. * * @param queryName name of a hibernate named query */ public void setQueryName(String queryName) { helper.setQueryName(queryName); }
/** * Update the context and clear the session if stateful. * * @param executionContext the current {@link ExecutionContext} * @throws ItemStreamException if there is a problem */ @Override public void update(ExecutionContext executionContext) throws ItemStreamException { super.update(executionContext); helper.clear(); }
/** * Open hibernate session and create a forward-only cursor for the query. */ @Override protected void doOpen() throws Exception { Assert.state(!initialized, "Cannot open an already opened ItemReader, call close first"); cursor = helper.getForwardOnlyCursor(fetchSize, parameterValues); initialized = true; }
@Override protected void doReadPage() { if (results == null) { results = new CopyOnWriteArrayList<T>(); } else { results.clear(); } results.addAll(helper.readPage(getPage(), getPageSize(), fetchSize, parameterValues)); }