Derik Whittaker

Syndication

News


PSA: NHibernate Session.Save() do you know what it returns?

Today I was working on some persistence code and quickly put together some code like the following:
Post Updated: Had the wrong code the first time.

        public HealthStatus SetStatusEntry( Int32 statusTypeId, DateTime statusDateTime )
        {
            var healthStatus = new HealthStatus() { StatusTypeId = statusTypeId, StatusDateTime = statusDateTime };

            var savedHealthStatus = (HealthStatus)Session.Save( healthStatus );

            return savedHealthStatus;
        }

When I ran my integration test to ensure the data was saved (yes, I am a bit anal) I was a bit perplexed by the exception which was being thrown “System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'FullNamspace.Goes.Here.HealthStatus'.”.  When i first looked at the code and the signature of .Save() all looked ok, then I realized that .Save() does not return the persisted entity but the ID of the entity that was just created.

If you are like me and wanted to return the fully persisted entity out of your method remember you can simply use the persisted entity and this will be good.

Hopes this save some hair for others.

Till next time,


Posted 09-15-2009 6:51 AM by Derik Whittaker
Filed under:

[Advertisement]

Comments

Matthew wrote re: PSA: NHibernate Session.Save() do you know what it returns?
on 09-15-2009 9:23 AM

Perhaps I'm missing something, but I don't see why you'd get the error you stated. Since you aren't returning the results of "Save" but instead returning the object you created, it should work as you expected. Did you perhaps place your FINAL code in the post and not the initial code that caused the problem?

Derik Whittaker wrote re: PSA: NHibernate Session.Save() do you know what it returns?
on 09-15-2009 9:36 AM

@Matthew,

I had put in the wrong sample code in the initial posting.  I have updated the code to be the 'broken' code.

Henning Anderssen wrote re: PSA: NHibernate Session.Save() do you know what it returns?
on 09-15-2009 9:41 AM

A bit of off-topic comment. Why not specify the StatusTypeId and StatusDateTime in the constructor?

In my opinion, when using object initializer that way, it is a good clue that you should introduce a constructor that takes in those arguments. Easier to control the invariants :)

Yearon wrote re: PSA: NHibernate Session.Save() do you know what it returns?
on 10-12-2009 3:04 AM

Great site. Keep doing.,

Doctorset wrote re: PSA: NHibernate Session.Save() do you know what it returns?
on 11-20-2009 5:45 PM

This is the welcome page for the dentaldoctor.us Association web site.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)