HibernateCursorItemReader<T> reader = new HibernateCursorItemReader<>(); reader.setFetchSize(this.fetchSize); reader.setParameterValues(this.parameterValues); reader.setQueryProvider(this.queryProvider); reader.setQueryName(this.queryName); reader.setQueryString(this.queryString); reader.setQueryProvider(provider); reader.setSessionFactory(this.sessionFactory); reader.setUseStatelessSession(this.useStatelessSession); reader.setCurrentItemCount(this.currentItemCount); reader.setMaxItemCount(this.maxItemCount); reader.setName(this.name); reader.setSaveState(this.saveState);
@Override protected ItemReader<Foo> getItemReader() throws Exception { SessionFactory sessionFactory = createSessionFactory(); String hsqlQuery = "from Foo"; HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReader<>(); reader.setQueryString(hsqlQuery); reader.setSessionFactory(sessionFactory); reader.setUseStatelessSession(true); reader.setFetchSize(10); reader.afterPropertiesSet(); reader.setSaveState(true); return reader; }
@Override protected void setQuery(HibernateCursorItemReader<Foo> reader) { reader.setQueryString("from Foo where name like :name"); reader.setParameterValues(Collections.singletonMap("name", "bar%")); }
@Test @SuppressWarnings("unchecked") public void testStatefulClose(){ SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); Query<Foo> scrollableResults = mock(Query.class); HibernateCursorItemReader<Foo> itemReader = new HibernateCursorItemReader<>(); itemReader.setSessionFactory(sessionFactory); itemReader.setQueryString("testQuery"); itemReader.setUseStatelessSession(false); when(sessionFactory.openSession()).thenReturn(session); when(session.createQuery("testQuery")).thenReturn(scrollableResults); when(scrollableResults.setFetchSize(0)).thenReturn(scrollableResults); itemReader.open(new ExecutionContext()); itemReader.close(); }
@Override protected ItemReader<Foo> createItemReader() throws Exception { LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMappingLocations(new ClassPathResource("Foo.hbm.xml", getClass())); customizeSessionFactory(factoryBean); factoryBean.afterPropertiesSet(); SessionFactory sessionFactory = factoryBean.getObject(); HibernateCursorItemReader<Foo> hibernateReader = new HibernateCursorItemReader<>(); setQuery(hibernateReader); hibernateReader.setSessionFactory(sessionFactory); hibernateReader.setUseStatelessSession(isUseStatelessSession()); hibernateReader.afterPropertiesSet(); hibernateReader.setSaveState(true); return hibernateReader; }
private void initializeItemReader(HibernateCursorItemReader<?> reader, String hsqlQuery) throws Exception { LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMappingLocations(new Resource[] { new ClassPathResource( "Foo.hbm.xml", getClass()) }); factoryBean.afterPropertiesSet(); SessionFactory sessionFactory = factoryBean .getObject(); reader.setQueryString(hsqlQuery); reader.setSessionFactory(sessionFactory); reader.afterPropertiesSet(); reader.setSaveState(true); reader.open(new ExecutionContext()); }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { HibernateCursorItemReader<Foo> reader = (HibernateCursorItemReader<Foo>) tested; reader.close(); reader.setQueryString("from Foo foo where foo.id = -1"); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
@Test public void testMultipleItemsInProjection() throws Exception { HibernateCursorItemReader<Object[]> reader = new HibernateCursorItemReader<>(); initializeItemReader(reader, "select f.value, f.name from Foo f"); Object[] foo1 = reader.read(); assertEquals(1, foo1[0]); }
/** * Exception scenario. * * {@link HibernateCursorItemReader#setUseStatelessSession(boolean)} can be * called only in uninitialized state. */ @Test public void testSetUseStatelessSession() { HibernateCursorItemReader<Foo> inputSource = (HibernateCursorItemReader<Foo>)reader; // initialize and call setter => error inputSource.open(new ExecutionContext()); try { inputSource.setUseStatelessSession(false); fail(); } catch (IllegalStateException e) { // expected } }
@Override protected void setQuery(HibernateCursorItemReader<Foo> reader) { reader.setQueryName("allFoos"); }
protected void setQuery(HibernateCursorItemReader<Foo> reader) throws Exception { reader.setQueryString("from Foo"); }
@Override protected void setQuery(HibernateCursorItemReader<Foo> hibernateReader) throws Exception { String nativeQuery = "select * from T_FOOS"; //creating a native query provider as it would be created in configuration HibernateNativeQueryProvider<Foo> queryProvider = new HibernateNativeQueryProvider<>(); queryProvider.setSqlQuery(nativeQuery); queryProvider.setEntityClass(Foo.class); queryProvider.afterPropertiesSet(); hibernateReader.setQueryProvider(queryProvider); } }
@Test public void testSingleItemInProjection() throws Exception { HibernateCursorItemReader<Object> reader = new HibernateCursorItemReader<>(); initializeItemReader(reader, "select f.value from Foo f"); Object foo1 = reader.read(); assertEquals(1, foo1); }
HibernateCursorItemReader<T> reader = new HibernateCursorItemReader<>(); reader.setFetchSize(this.fetchSize); reader.setParameterValues(this.parameterValues); reader.setQueryProvider(this.queryProvider); reader.setQueryName(this.queryName); reader.setQueryString(this.queryString); reader.setQueryProvider(provider); reader.setSessionFactory(this.sessionFactory); reader.setUseStatelessSession(this.useStatelessSession); reader.setCurrentItemCount(this.currentItemCount); reader.setMaxItemCount(this.maxItemCount); reader.setName(this.name); reader.setSaveState(this.saveState);
@Test public void testSingleItemInProjectionWithArrayType() throws Exception { HibernateCursorItemReader<Object[]> reader = new HibernateCursorItemReader<>(); initializeItemReader(reader, "select f.value from Foo f"); try { Object[] foo1 = reader.read(); assertNotNull(foo1); fail("Expected ClassCastException"); } catch(ClassCastException e) { // expected } }