2009-04-21 161 views
0

我想用NHibernate連接到Northwind數據庫。但由於某種原因,我無法加載實體類型。NHibernate映射錯誤

這是我的實體類

public class Product 
    { 
     public virtual Int32 ProductId { get; set; } 
     public virtual String Desc { get; set; } 
    } 

這裏是我的映射

<class name="Product" table="Products"> 
    <id name="ProductId" column="ProductId" type="Int32"> 
     <generator class="identity"></generator> 
    </id> 
    <property name="Desc" column="ProductName" type="String" length="60"> 
    </property> 
    </class> 

我收到以下錯誤消息

無法加載實體:OracleLinq.Product#12 ] [SQL:SELECT product0_.ProductId as ProductId0_0_,product0_.ProductName as ProductN2_0_0_ FROM Products product0_ WHERE product0_.ProductId =?]

這裏是堆棧跟蹤

at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister) 
    at NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId) 
    at NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session) 
    at NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session) 
    at NHibernate.Event.Default.DefaultLoadEventListener.LoadFromDatasource(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) 
    at NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) 
    at NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) 
    at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType) 
    at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType) 
    at NHibernate.Impl.SessionImpl.ImmediateLoad(String entityName, Object id) 
    at NHibernate.Proxy.AbstractLazyInitializer.Initialize() 
    at NHibernate.Proxy.AbstractLazyInitializer.GetImplementation() 
    at NHibernate.Proxy.Poco.Castle.CastleLazyInitializer.Intercept(IInvocation invocation) 
    at Castle.DynamicProxy.AbstractInvocation.Proceed() 
    at ProductProxy4c67cf5bf6e640ab82d8c21a90e2a62b.set_Desc(String value) 
    at OracleLinq.Form1.Form1_Load(Object sender, EventArgs e) 

難道我做錯了什麼?

+1

異常內部是否存在內部異常? – 2009-04-21 21:47:47

+1

通常每當我得到這個nhibernate錯誤時,我都會嘗試在Sql Management Studio中複製並粘貼SQL(使用替換的參數),以確保我沒有做一些愚蠢的事情並拼錯一列。 – Min 2009-04-23 21:55:45

回答

0

您的連接字符串是否默認爲正確的數據庫?檢查確認其初始目錄= [DB這裏名稱]

+0

是的。我可以打開數據庫連接。這裏是我使用的代碼 配置cfg = new Configuration() cfg.Configure(); cfg.AddAssembly(typeof(Product).Assembly); ISessionFactory sessionFactory = cfg.BuildSessionFactory(); IDbConnection conn = new SqlConnection(@「Data Source =(local); Initial Catalog = Northwind; Integrated Security = SSPI;」); ISession session = sessionFactory.OpenSession(conn); Product product =(Product)session.Load(typeof(Product),12); product.Desc =「」; – Seshan 2009-04-21 19:59:41

0

這實際上應該只有在插入時有問題,但你也可以得到它的煩惱:

我覺得「身份」不受Oracle支持,它是一個SqlServer功能(一個自動計數主鍵)。 Oracle使用序列。

請嘗試以下(檢查順序,必須存在的名稱)

<id name="ProductId" column="ProductId" type="Int32"> 
    <generator class="sequence"> 
    <param name="sequence">product_seq</param> 
    </generator> 
</id> 

或其它ID生成。 Hilo或Guid是有趣的選擇。見http://barchitect.blogspot.com/2008/07/nhibernate-generator-and-primary-key.html

1

第二個嘗試:

這裏是你的意見,你的配置,只是可讀性:

Configuration cfg = new Configuration(); 
cfg.Configure(); 
cfg.AddAssembly(typeof(Product).Assembly); 
ISessionFactory sessionFactory = cfg.BuildSessionFactory(); 

IDbConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;"); 
ISession session = sessionFactory.OpenSession(conn); 

Product product = (Product)session.Load(typeof(Product), 12); 
product.Desc = ""; 

很顯然,NHibernate的有映射文件,它不能生成的查詢。

這可能是

  • 數據庫是不是有:你已經籤這個。
  • 該表不存在:打開一個sql控制檯(使用相同的連接字符串)並將錯誤消息中的sql複製粘貼到其中。它工作嗎?
  • 連接不打開:看bollow

我認爲你需要自己打開連接。更好的是讓NHibernate創建和管理連接。

試試這個:

Configuration cfg = new Configuration(); 
cfg.Configure(); 
cfg.AddAssembly(typeof(Product).Assembly); 
ISessionFactory sessionFactory = cfg.BuildSessionFactory(); 

ISession session = sessionFactory.OpenSession(); 

Product product = (Product)session.Load(typeof(Product), 12); 
product.Desc = ""; 

的CONNECTSTRING轉到nhibernate.cfg.xml

<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property> 
<property name="connection.connection_string"> 
Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI; 
</property> 
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property> 
0

在你的堆棧跟蹤,我看到ProductProxy4c67cf5bf6e640ab82d8c21a90e2a62b.set_Desc(String value),這意味着發生錯誤時,你設置的「說明'財產。這是NHibernate嘗試從數據庫加載產品的時刻。

在您的數據庫中,您沒有提供給Session.Load的id的產品。如果你使用Session.Get,你可能會得到空值。