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.  [ 4 posts ] 
Author Message
 Post subject: Using NHibernate without a database
PostPosted: Mon Mar 05, 2007 7:52 am 
Newbie

Joined: Mon Mar 05, 2007 3:50 am
Posts: 6
Hi all,

Q: Is it possible to use NHibernate in a "database-less" mode? The reason is that I want to write some unit test for my business logic without dependency on database server resources. Ideally, I would write the test setup code that would populate the ISession (or perhaps a second level cache) and just pass it to the business logic that would load or query entities (i.e business as usual).

I know some recommend implement test version of the DAOs involved, but I'm reluctant to do that, because it would mean implementing a lot of non-trivial fake querying code.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 6:11 pm 
Newbie

Joined: Mon Mar 05, 2007 3:50 am
Posts: 6
After some consideration, I decided to go with SQLite for my unit tests. The setup speed seems reasonable.

Code:
         string dirpath = @"C:\NHibernateDatabase";
         Directory.CreateDirectory(dirpath);

         string filename = dirpath + @"\"
            + DateTime.Now.ToString("yyyyMMdd-HHmmss-")
            + (DateTime.Now.Ticks % 1000).ToString() + ".db";
         System.Diagnostics.Debug.WriteLine("Generating " + filename);

         string connectionString = "Data Source=" + filename + ";Version=3";

         Hashtable properties = new Hashtable();
         properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
         properties.Add("hibernate.dialect", "NHibernate.Dialect.SQLiteDialect");
         properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SQLite20Driver");
         properties.Add("hibernate.connection.connection_string", connectionString);
         properties.Add("hibernate.query.substitutions", "true=1;false=0");

         NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();
         configuration.Properties = properties;

         NHibernate.Mapping.Attributes.HbmSerializer serializer = NHibernate.Mapping.Attributes.HbmSerializer.Default;
         serializer.Validate = true;

         configuration.AddInputStream(serializer.Serialize(inAssembly));

         SchemaExport export = new SchemaExport(configuration);

         SQLiteConnection connection = new SQLiteConnection(connectionString);
         connection.Open();
         export.Execute(false, true, false, false, connection, null);
         connection.Close();

         mSessionFactory = configuration.BuildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 6:22 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
I think you could also add this property to the configuration to have the schema exported automatically

properties.Add("hibernate.hbm2ddl.auto", "create-drop");


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 6:38 pm 
Newbie

Joined: Mon Mar 05, 2007 3:50 am
Posts: 6
Yup, that worked nicely. Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.