NHibernate中的映射错误

时间:2009-04-21 18:07:55

标签: nhibernate

我正在尝试使用NHibernate连接到Northwind数据库。但由于某种原因,我无法加载实体类型。

这是我的实体类

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)

我做错了吗?

4 个答案:

答案 0 :(得分: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 = "";

连接字符串转到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>

答案 1 :(得分:0)

您的连接字符串是否默认为正确的数据库?检查它是否有初始目录= [此处为DB名称]

答案 2 :(得分:0)

插入时实际上只是一个问题,但你也可能遇到麻烦:

我认为oracle不支持“identity”,它是一个SqlServer功能(自动计数主键)。 Oracle使用序列。

尝试以下方法(检查序列的名称,必须存在)

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

或其他id生成器。希洛或Guid是有趣的替代品。见http://barchitect.blogspot.com/2008/07/nhibernate-generator-and-primary-key.html

答案 3 :(得分:0)

在您的堆栈跟踪中,我看到ProductProxy4c67cf5bf6e640ab82d8c21a90e2a62b.set_Desc(String value),这意味着当您设置'Desc'属性时会发生错误。这是NHibernate尝试从数据库加载产品的那一刻。

在您的数据库中,您没有带有您提供给Session.Load的ID的产品。如果你使用Session.Get,你可能会得到null。

相关问题