我只是想開始學習NHibernate,並與一個非常簡單的POCO測試時,我已經有問題。我發現了沒有留存例外,這裏是我的代碼如下所示:NHibernate映射異常。沒有persister:NHibernateTesting.Account
Account
表:
create table Account
(
AccountID int primary key identity(1,1),
AccountName varchar(10),
CreateDate datetime
)
go
Account
類:
public class Account
{
public virtual int AccountID { get; set; }
public virtual string AccountName { get; set; }
public virtual DateTime CreateDate { get; set; }
public Account()
{
AccountID = 0;
AccountName = "";
CreateDate = new DateTime();
}
}
映射文件,Account.hbm.xml
(是的,它嵌入進入組裝):
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernateTesting" assembly="NHibernateTesting">
<class name="Account" table="Account">
<id name="AccountID">
<generator class="native"/>
</id>
<property name="AccountName" />
<property name="CreateDate" />
</class>
</hibernate-mapping>
Co在配置文件nfig部分:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">My connection string</property>
<mapping assembly="NHibernateTesting" />
</session-factory>
</hibernate-configuration>
最後,進行調用的代碼:
using (var session = NHibernateHelper.GetASession())
{
using (var tran = session.BeginTransaction())
{
var newAccount = new Account();
newAccount.AccountName = "some name";
session.Update(newAccount); // Exception thrown here.
tran.Commit();
}
}
有人能看到我在做什麼錯誤或我爲什麼會得到這個例外,我讀過在網絡上,這個問題與映射有關,但我在這個例子中看不到什麼錯誤。
也許Hibernate的Session是沒有正確配置?你能否給出來源NHibernateHelper? – proskor
順便說一句,每當你創建一個新的實例,你應該調用session.Save或session.SaveOrUpdate而不是session.Update。 – proskor