原來一直使用vs2003和Nhibernate1.02,項目需要升級到.Net2.0並開發WinFrom,相應的Nhibernate也需要升級到 1.2以上版本。
開始配置就遇上了一系列的問題,記錄在此,希望對各位有用。
問題一:未能找到元素“urn:nhibernate-mapping- 2.0:hibernate-mapping
解決方案:將類的mapping XML的“urn:nhibernate-mapping- 2.0"中的2.0替換為2.2
問題二:The dialect was not set. Set the property hibernate.dialect.
解決方案:明明在×××.exe.config中已經配置了Nhibernate的相關參數,為什麼會出現這樣的錯誤提示呢?原來是沒有將其自動復制到程序的運行目錄。選中該文件,查看屬性,其中第一項選擇為“始終復制”,問題解決。看樣子vs2005用的還是少啊。
問題三:method get_Id should be virtual
解決方案:原因是Nhibernate1.2使用了延遲加載。參照其文檔,解決方案有三種。
修改配置的XML,將類定義增加lazy=false;修改Nhibernate全局配置,增加default-lazy="false";將屬性的存取修改為Virtual,並且該類不能為sealed。
相關原文如下:
1. You can follow the advice of the exception and add "virtual" to all of your propertIEs, and make sure your class is non-sealed. Obviously you''ll want to do this if you think you might want to take advantage of the lazy-initializing proxy feature. However, changing your classes may not be practical or advisable if you have a legacy codebase, or it may just bother you that a "transparent" persistence framework is dictating how you design certain ASPects of your value classes. That''s where Options 2 and 3 come in. Both of those involve changing back to the old behavior.
2. To change the lazy-initialization proxy setting for a specific class, you can add a "lazy=''false''" attribute to the <class> mapping element. This might look something like:
<class
name="NorthwindClasses.Category, NorthwindClasses"
table="CategorIEs"
lazy="false">
3. To change the lazy-initialization proxy setting for all classes in a given mapping file, you can add a "default-lazy=''false''" attribute to the <hibernate-mapping> element,