Maxime ARNSTAMM
Maxime ARNSTAMM

Reputation: 5314

How can i verify that all my tables are empty with HQL/Hibernate?

I have a big functionnal test suite. Each test are independant. But, from time to time, there is a problem and one of these tests leaks (exception happens before the data could be cleaned etc) and some of the subsequent tests could fail.

So the first thing i want to do in each of these test is check if the database is really empty. It would help to know that the breakage is due to a leak and not a regression.

I would like to know if i can do this with a single request, because I'd like to avoid doing and maintaining something like :

sessionFactory.getCurrentSession().createQuery("Select a From A a").list().isEmpty()
...
sessionFactory.getCurrentSession().createQuery("Select z From Z z").list().isEmpty()

I found this for mysql : List of non-empty tables in MySQL database but it is specific.

Thanks :)

Upvotes: 2

Views: 1659

Answers (2)

bvanvelsen - ACA Group
bvanvelsen - ACA Group

Reputation: 1751

What I do is set my "hibernate.hbm2ddl.auto" to create in the hibernate.cfg.xml and in each test I reset my sessionfactory using

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

This way the database is recreated for each test

Upvotes: 1

aviad
aviad

Reputation: 8278

How about

session.createQuery("select 1 from table").setMaxSize(1).list().isEmpty()

Upvotes: 0

Related Questions