{"id":789,"date":"2010-04-02T11:09:32","date_gmt":"2010-04-02T09:09:32","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=789"},"modified":"2010-04-02T11:09:32","modified_gmt":"2010-04-02T09:09:32","slug":"isession-load-returns-object-with-zero-id","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/isession-load-returns-object-with-zero-id","title":{"rendered":"ISession.Load returns object with zero ID"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/12\/nhibernate.gif\" alt=\"\" title=\"nhibernate\" width=\"204\" height=\"125\" class=\"alignleft size-full wp-image-365\" \/><br \/>\nRecently we had a following problem with <strong>NHibernate<\/strong>, and although I love NHiberante, it does not always behave as expected.<\/p>\n<p>We have a Person class correctly mapped in NHibernate:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Person\n{\n\tpublic int Id { get; set; }\n\tpublic int Name { get; set; }\n}\n<\/pre>\n<p>We saw that sometimes we were getting a Person with Id equal to zero, from our repository:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class PersonRepository\n{\n\tprivate ISession _session;\n\n\t\/\/...\n\n\tpublic Person LoadById(int id)\n\t{\n\t\treturn _session.Load&lt;person&gt;(id);\n\t}\n}\n<\/pre>\n<p>We narrowed the problem down and wrote this little test:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;Test]\npublic void LoadById_Loads_IdIsSet()\n{\n    _context.ExecuteInTransaction(() =&gt;\n\t\t{\n\t\t\tPerson person = _context.PersonRepository.LoadById(7);\n\t\t\tAssert.AreEqual(7, person.Id);\n\t\t}\n\t);\n}\n<\/pre>\n<p>&#8230;which of course failed.<\/p>\n<p>After initial surprise, we took a second look at the Person class and we saw that we are missing <strong>virtual <\/strong> keyword on properties. NHibernate is not able to create a correct Proxy object.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Person\n{\n\tpublic virtual int Id { get; set; }\n\tpublic virtual int Name { get; set; }\n}\n<\/pre>\n<p>This fixed the issue.<br \/>\nStrange thing is that we expect that NHibernate would throw an exception is such case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently we had a following problem with NHibernate, and although I love NHiberante, it does not always behave as expected. We have a Person class correctly mapped in NHibernate: public class Person { public int Id { get; set; } public int Name { get; set; } } We saw that sometimes we were getting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[38],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-programming","tag-nhibernate"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/789"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=789"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/789\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}