1

I'm trying to test my code using Spring and DBunit (http://springtestdbunit.github.io/)

Once inside the unit test:

sessionFactory.getCurrentSession().createCriteria(MyEntity.class).list()

will return the list of entities inserted by DBUnit, so it did insert records in teh database (MYSQL).

But:

sessionFactory.openStatelessSession().createCriteria(MyEntity.class).scroll(FORWARD_ONLY).next()

will return false! It can't find any records using the stateless session.

Beforehand, I use to insert the same records using Liquibase, and this piece of code worked perfectly.

Using HSql it's even worse, it totally freezes when trying to open the stateless session... (on the scroll() instruction...)

Thank you for your help!

Chaps
  • 119
  • 1
  • 11

3 Answers3

0

OK! So this actually have nothing to do with DBunit, sort of...

This code simple shows the same issue, and no DBUnit involve:

MyEntity myEntity = new MyEntity ();
    sessionFactory.getCurrentSession().save(myEntity );
    assertEquals(1, sessionFactory.getCurrentSession().createCriteria(MyEntity .class).list()
            .size());
    assertTrue(sessionFactory.openStatelessSession().createCriteria(MyEntity .class)
            .scroll(ScrollMode.FORWARD_ONLY).next());

First assert is ok, second fails. So I guess it is a simple hibernate problem, and I also guess that liquibase was doing something to prevent it somehow... Probably a transaction issue?

Chaps
  • 119
  • 1
  • 11
0

SO! It seems to be a transaction issue in the end as a simple

sessionFactory.getCurrentSession().getTransaction().commit();

make the test go green!

Question now is how to make DBUnit commit the transaction I suppose...

At the beginning of the test, after DBUnit initialization, I run:

sessionFactory.getCurrentSession().getTransaction().wasCommitted()

And it returns false. This think the issue comes from there.

Chaps
  • 119
  • 1
  • 11
0

Problem solved! Thanks to this: https://tadaya.wordpress.com/2008/04/27/transaction-aware-datasource-use-dbunit-hibernate-in-spring/

Chaps
  • 119
  • 1
  • 11