public void testInterceptorWithThreadBoundAndFlushAlways() { MockControl sfControl = MockControl.createControl(SessionFactory.class); SessionFactory sf = (SessionFactory) sfControl.getMock(); MockControl sessionControl = MockControl.createControl(Session.class); Session session = (Session) sessionControl.getMock(); session.getSessionFactory(); sessionControl.setReturnValue(sf, 1); session.isOpen(); sessionControl.setReturnValue(true, 1); session.getFlushMode(); sessionControl.setReturnValue(FlushMode.AUTO, 1); session.setFlushMode(FlushMode.ALWAYS); sessionControl.setVoidCallable(1); session.setFlushMode(FlushMode.AUTO); sessionControl.setVoidCallable(1); sfControl.replay(); sessionControl.replay(); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setSessionFactory(sf); interceptor.setFlushMode(HibernateInterceptor.FLUSH_ALWAYS); try { interceptor.invoke(new TestInvocation(sf)); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } finally { TransactionSynchronizationManager.unbindResource(sf); } sfControl.verify(); sessionControl.verify(); }
public void testInterceptorWithNewSessionAndFlushNever() throws HibernateException { MockControl sfControl = MockControl.createControl(SessionFactory.class); SessionFactory sf = (SessionFactory) sfControl.getMock(); MockControl sessionControl = MockControl.createControl(Session.class); Session session = (Session) sessionControl.getMock(); sf.openSession(); sfControl.setReturnValue(session, 1); session.getSessionFactory(); sessionControl.setReturnValue(sf); session.setFlushMode(FlushMode.MANUAL); sessionControl.setVoidCallable(1); session.close(); sessionControl.setReturnValue(null, 1); sfControl.replay(); sessionControl.replay(); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setFlushModeName("FLUSH_NEVER"); interceptor.setSessionFactory(sf); try { interceptor.invoke(new TestInvocation(sf)); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } sfControl.verify(); sessionControl.verify(); }
@Override public void validate(Object obj, Errors errors, boolean cascade) { final Session session = sessionFactory.getCurrentSession(); FlushMode previousMode = null; try { if (session != null) { previousMode = session.getFlushMode(); session.setFlushMode(FlushMode.MANUAL); } super.validate(obj, errors, cascade); } finally { if (session != null && previousMode != null) { session.setFlushMode(previousMode); } } }
@Test private void testALWAYSFlushMode() { Session session = sessionFactory.getCurrentSession(); session.setFlushMode(FlushMode.ALWAYS); session.beginTransaction(); incomesEntity = (IncomesEntity) session.createCriteria("incomesEntity").setMaxResults(1).uniqueResult(); incomesEntity.setSum(100); session.getTransaction().commit(); Assert.assertNotNull(session); }