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: Remove all objects
PostPosted: Sun Nov 18, 2007 9:02 pm 
Newbie

Joined: Sun Aug 20, 2006 8:09 pm
Posts: 8
Hi everybody!
Is there a way to remove all objects ( e.g. entries in the db-tables) via NHibernate? This would be useful for my testcode, since it needs the db to be in a known state.
If i query for all objects and try to remove them in a foreach loop, I always encounter foreign key constraint exceptions from the database.

I'm using PostgreSQL 8.2 and NHibernate 1.2.0 GA

Any ideas are welcome.
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 18, 2007 11:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
For each mapped class, do this:
Code:
session.Delete("from MyEntityClass");

You will have to delete the child classes first to avoid violating foreign key constraint; but at least you are managing it at the class level and not on the object level. It's also not the most efficient, since NHibernate loads each object before deleting it. However, it may suffice for testing when you don't have a lot of data.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 3:25 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
I use SchemaExport to blast the db and rebuild the schema for testing...

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 5:11 am 
Beginner
Beginner

Joined: Mon Oct 22, 2007 5:44 am
Posts: 28
Hi,

For testing, you can simply run a sql script to drop all the tables.
If you need detailed example, let me know.

_________________
dyahav


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 4:11 pm 
Newbie

Joined: Sun Aug 20, 2006 8:09 pm
Posts: 8
dyahav wrote:
Hi,

For testing, you can simply run a sql script to drop all the tables.
If you need detailed example, let me know.


yeah, that's actually my plan if everything else fails. The drawback is, that you got to bypass NHibernate and directly access the db. But I guess thats ok for test code.

Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 2:33 am 
Beginner
Beginner

Joined: Mon Oct 22, 2007 5:44 am
Posts: 28
Hi Poseidon,

If you use Oracle, you can run the following sql script:

set echo off
set heading off
spool temp.sql;
select 'drop table ' || table_name || ' cascade constraints ;' from user_tables;
spool off;
set echo on
set heading on
@temp
exit;


-- You can run this script from a batch file that can be issued from your
program code.

my batch file looks like this:
sqlplus -S UserName/Passwd@ServerName/DBName @drop_tables.sql

I hope you will find it helpful.

_________________
dyahav


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.