-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Unit-Testing: Simple way to remove data from all tables?
PostPosted: Thu Mar 02, 2006 6:53 am 
Newbie

Joined: Thu Mar 02, 2006 6:29 am
Posts: 4
Hibernate version: 3.1.1

Hi,
I am currently Unit-Testing our Hibernate Persistence Layer. My setup() Method usually contains method calls to delete data from all tables which are affected by these tests. E.g.
Code:
      Session session = HibernateManager.getTheManager().getSession();
      Transaction transaction = session.beginTransaction();
      session.createQuery("delete from Application").executeUpdate();
      transaction.commit();


In my opinion, a method to clear all tables that are used by Hibernate would be much more convenient. A simple way to achieve something like that would be to allow wildcards in hql (like * for all Types):
Code:
session.createQuery("delete from *")


It is obvious that this does not work, at least currently. What are your ideas? How do you delete the contents of all tables?

Another way to delete everything is to drop and re-create all tables via hbm2ddl (HibernateTools), but this approach simply takes to long for unit-testing :(

Thanks in advance for all comments and help!

Kind Regards,
Matthias


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:21 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
I use
simple delete from table name.
You could recreate your tables as well when your db is not large enough. Problem is that during development you might want some tables to stay (configuration tables) and other to be cleaned. I do not think that you can do this manually.

A delete cascade table definition (not in Hibernate but in the DB) would work as well.
Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 12:56 pm 
Newbie

Joined: Thu Mar 02, 2006 6:29 am
Posts: 4
Thanks for your Response!

LaLiLuna wrote:
I use
simple delete from table name.

That is what I am doing right now. The problem is that I have to maintain this "cleanup"-Method whenever I add a new persistent Class. That is why I had the idea about wildcards. An Operation like Hibernate.cleanAllTables() would be great, too :)

Quote:
You could recreate your tables as well when your db is not large enough. Problem is that during development you might want some tables to stay (configuration tables) and other to be cleaned. I do not think that you can do this manually.

Dropping all Tables and recreating them takes too long for unit-testing, there are too many tables :(

Quote:
A delete cascade table definition (not in Hibernate but in the DB) would work as well.

Unfortunately, this is not possible because the definition gets created from *.hbm.xml mapping-files when creating the SessionFactory (via hbm2ddl) before running all the tests.

Kind regards,
Matthias


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 12:58 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
you could read the meta data from your configuration and loop over all classes.

Best REgards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 4:51 am 
Newbie

Joined: Thu Mar 02, 2006 6:29 am
Posts: 4
Thank your for that simple but very nice solution!

:)

Kind Regards,
Matthias


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 04, 2006 9:31 am 
Newbie

Joined: Thu Mar 02, 2006 6:29 am
Posts: 4
I just found an even easier solution! There might be someone interested in this, so here it is:

Code:
      Session session = HibernateManager.getTheManager().getSession();
      Transaction transaction = session.beginTransaction();
      List all = session.createQuery("from java.lang.Object").list();
      Iterator it = all.iterator();
      while (it.hasNext()) {
         session.delete(it.next());
      }
      
      transaction.commit();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.