Set Hibernate session's flush mode in Spring

Posted by glaz666 on Stack Overflow See other posts from Stack Overflow or by glaz666
Published on 2010-10-28T09:48:28Z Indexed on 2012/09/29 15:38 UTC
Read the original article Hit count: 184

Filed under:
|
|

I am writing integration tests and in one test method I'd like to write some data to DB and then read it.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@TransactionConfiguration()
@Transactional
public class SimpleIntegrationTest {

    @Resource
    private DummyDAO dummyDAO;

    /**
     * Tries to store {@link com.example.server.entity.DummyEntity}.
     */
    @Test
    public void testPersistTestEntity() {
        int countBefore = dummyDAO.findAll().size();
        DummyEntity dummyEntity = new DummyEntity();
        dummyDAO.makePersistent(dummyEntity);

        //HERE SHOULD COME SESSION.FLUSH()

        int countAfter = dummyDAO.findAll().size();

        assertEquals(countBefore + 1, countAfter);
    }
}

As you can see between storing and reading data, the session should be flushed because the default FushMode is AUTO thus no data can be actually stored in DB.

Question: Can I some how set FlushMode to ALWAYS in session factory or somewhere else to avoid repeating session.flush() call?

All DB calls in DAO goes with HibernateTemplate instance.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about spring