-->
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.  [ 5 posts ] 
Author Message
 Post subject: Change mapping files after application deployment
PostPosted: Wed Oct 01, 2008 6:31 am 
Newbie

Joined: Wed Oct 01, 2008 6:07 am
Posts: 7
Scenario – windows based application using nhibernate for CRUD operations and when this application gets deployed requirement exists that if a database table column gets removed application should ignore that column and work (normally Save,update,delete commands should work).
We applied nhibernate to cope up with this requirement and ended up noticing that you cannot actually change the mapping files (hbm.xml) after you deploy the application since they are embedded to the assembly (since the build action is set to embedded resource).

Is there any workaround for this to change mapping files after deploying application (without rebuilding and replacing the assembly)?

Ex- User table mapping

<property name="UserID" column="UserID" />
<property name="LastName" column="LastName" />
<property name="FirstName" column="FirstName" />
<property name="Title" column="Title" />

Remove the Title column from the table

In order to save operation to work title mapping should be removed. But since it’s embedded in to the assembly cannot do so.
Any workaround to change mapping files?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 7:16 am 
Newbie

Joined: Fri Sep 28, 2007 4:12 am
Posts: 15
It is possible to have the .hbm.xml files lying around as files in the application directory. I usually have a subfolder where all the .xml files reside that have the server connection info in them, in that folder there are subfolders with the <connection info filename>\classxyz.hbm.xml files for a particular database. All those files are set to be content and not embedded resource.

that way you can change the hbm.xml files without recompile, just adding new fields obviously won't work ;)

the following is my central function that returns an nhibernate sessionfactory based on the requested database connection file

copy&paste galore

Code:
        private ISessionFactory GetSessionFactoryFor(string psessionfactoryconfigpath)
      {
            Check.Require(!string.IsNullOrEmpty(psessionfactoryconfigpath), "sessionFactoryConfigPath may not be null nor empty");
         ISessionFactory lsessionfactory = null;
            //  Attempt to retrieve a stored SessionFactory from the hashtable containing all factories keyed with their config filename.
         // thread safety
         lock (sessionFactories)
         {

             lsessionfactory = (ISessionFactory)sessionFactories[psessionfactoryconfigpath];
            //  Failed to find a matching SessionFactory so make a new one.
            if (lsessionfactory == null)
            {
               Check.Require(File.Exists(psessionfactoryconfigpath), "The config file at '" + psessionfactoryconfigpath + "' could not be found");

               // first create the nhibernate config
               NHibernate.Cfg.Configuration lconfiguration = new NHibernate.Cfg.Configuration();
               lconfiguration.Configure(psessionfactoryconfigpath);

               // now get every hbm.xml file that resides inside the directory named like the database config file
               string ldomainobjectbasepath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(psessionfactoryconfigpath), System.IO.Path.GetFileNameWithoutExtension(psessionfactoryconfigpath));
               string[] ldomainobjects = System.IO.Directory.GetFiles(ldomainobjectbasepath, "*.hbm.xml");
               foreach (string lcurrentdomainobject in ldomainobjects)
               {
                  lconfiguration.AddFile(lcurrentdomainobject);
               }
               lsessionfactory = lconfiguration.BuildSessionFactory();
               if (lsessionfactory == null)
               {
                  throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
               }
               sessionFactories.Add(psessionfactoryconfigpath, lsessionfactory);
            }
         }
            return lsessionfactory;
        }


Top
 Profile  
 
 Post subject: Changing mapping files
PostPosted: Thu Oct 02, 2008 12:56 am 
Newbie

Joined: Wed Oct 01, 2008 6:07 am
Posts: 7
thanks mosi. It works great,
one small clarification that i was previously using
config.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly())
to add embedded resources, with this i can omit that statement isn't it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 2:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Yes, you don't need that anymore.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 4:11 am 
Newbie

Joined: Fri Sep 28, 2007 4:12 am
Posts: 15
Happy to help :)

Yep as wolli said you don't need that line anymore. The .hbm.xml file contains enough info for nhibernate to find the appropriate class from your assembly. It can even be in a completely different assembly as long as its publicly accessible.

Oh btw: since the connection files with usernames etc. are on your disk now, watch out where you put them if you use this in a web application. I suggest App_Data to make things more secure.


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