Geeks With Blogs

News

Len Smith's Facebook profile


nIgnuSharp.NET //len smith is a software developer in ann arbor, mi
Some things to watch out for when working with NHibernate...

If you update an NHibernate entity without detaching it first from the session, it will be updated automatically.


         [Test]        
        
public void UpdatedEntityPropertyWillSaveAutomatically()
        {
            Customer customer 
= null;

            try
            
{                
                customer 
_Repository.GetFirst();
                
customer.FirstName = null;  // First Name can not be null, so this will throw an exception
                
_Repository.Flush();  // flush happens also when the session ends.
            
}
            
catch(Exception ex)
            {
                Assert.That(ex.InnerException 
is System.Data.SqlClient.SqlException);
                
_Repository.Clear();                              
                return;
            
}

            Assert.Fail(
"We should never get here, because flushing the repository will throw the exception above.");
        
}

If you do not detach your object from its repository, NHibernate will pass you a reference to the same object.

        [Test]
        
public void IfNotDetachedTwoObjectsHaveTheSameReference()
        {
            Customer c1 
_Repository.GetFirst();
            
Customer c2 _Repository.GetById(c1.ID);

            
c1.LastName +"1";
            
Assert.AreEqual(c1.LastName, c2.LastName);
        
}

        [Test]
        
public void IfDetachedAnObjectWillNotHaveTheSameReference()
        {
            Customer c1 
_Repository.GetFirst();
            
_Repository.Evict(c1);
            
Customer c2 _Repository.GetById(c1.ID);

            
c1.LastName +"1";
            
Assert.AreNotEqual(c1.LastName, c2.LastName);
        
}
Posted on Thursday, February 21, 2008 11:33 AM | Back to top


Comments on this post: A Few NHibernate Gotchas

# re: A Few NHibernate Gotchas
Requesting Gravatar...
I read the article and it helped and I found NetSpell to be an excellent free spelling engine that works both in ASP.Net and Windows forms.
Left by Mosaic tables on Nov 13, 2009 1:50 AM

# re: A Few NHibernate Gotchas
Requesting Gravatar...
Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work... we are major supplier of used clothing, used shoes, garments and many different kinds of name brand clothing closeouts etc., many more are available here...
Left by static caravan insurance on Feb 27, 2010 1:13 AM

# re: A Few NHibernate Gotchas
Requesting Gravatar...
Firstly, You've got a lot of fantastic information here on your web site. I am very impressed! If we consider the part of Link building which is an important part of Search Engine Optimization and anyone who has website and runs the business online, consider various online marketing strategies to market the business and gain more profits.
Left by contextual links on Mar 01, 2010 12:03 AM

# re: A Few NHibernate Gotchas
Requesting Gravatar...
Resources like the one you mentioned here is very useful! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
Left by SMO Dubai on Mar 09, 2010 4:14 AM

# re: A Few NHibernate Gotchas
Requesting Gravatar...
i like this Gucci new design handbags,see here
Left by GucciBags on Oct 05, 2010 11:02 PM

Your comment:
 (will show your gravatar)


Copyright © Len Smith | Powered by: GeeksWithBlogs.net