NHibernate -在查询中问号代替值

本文关键字:查询 NHibernate | 更新日期: 2023-09-27 18:07:20

基本上我有一个Topic对象,它可以有多个Category与它相关联。我试图插入一个新的Category到一个特定的Topic,我得到一个错误。

使用ASP.NET 4,前端也使用windows-1255编码,ASPhebrew_binMySQL(后端)

ISession session = dal.GetSession();
using (session.BeginTransaction())
{
    Topic t = session.Get<Topic>(topicId);
    Category c = new Category() { Name=name };
    t.AddCategory(c); // updates both references (inside `t` and inside `c`)
    session.Update(t);
    session.Save(c);
    session.Transaction.Commit();
}
得到错误:

无法插入:[DAL.Models.][SQL: INSERT INTO Category (Name, Publish, TopicID, ID) VALUES (?, ?, ?, ?)]

最终失败是因为违反了外键约束(我猜?不是有效的ID):

不能添加或更新子行:外键约束失败(etladaatdb . c)。category,约束FK6482F249B6E2851外键(ID)引用topic (ID)

Topic.cs:

public enum ColorEnum { Blue, Red, Green, Yellow }
    public class Topic
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual string ImageUri { get; set; }
        public virtual ColorEnum Color { get; set; }
        public virtual IList<Category> Categories { get; set; }
        public virtual void AddCategory(Category c)
        {
            Categories.Add(c);
            c.Topic = this;
        }
    }

Topic.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="DAL"
                   namespace="DAL.Models">
  <class name="Topic" lazy="true">
    <id name="ID">
      <generator class="increment"></generator>
    </id>
    <property name="Name" />
    <property name="Description" />
    <property name="ImageUri" />
    <property name="Color" type="ColorEnum" />
    <bag name="Categories" lazy="true" inverse="true"
                         batch-size="25" cascade="all-delete-orphan">
      <key column="ID" />
      <one-to-many class="Category" />
    </bag>
  </class>
</hibernate-mapping>

Category.cs:

public class Category
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual bool Publish { get; set; }
        public virtual Topic Topic { get; set; }
    }

Category.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="DAL"
                   namespace="DAL.Models">
  <class name="Category" lazy="true">
    <id name="ID">
      <generator class="increment"></generator>
    </id>
    <property name="Name"/>
    <property name="Publish" />
    <many-to-one name="Topic" class="Topic" column="TopicID" />
  </class>
</hibernate-mapping>

NHibernate -在查询中问号代替值

看起来你真的错过了给类别分配主题。所以这个应该能达到这个效果:

// original
Category c = new Category() { Name=name };
t.AddCategory(c);
// always assign both sides during creation
c.Topic = t;
现在NHibernate将知道什么是被引用的 Topic …并将正确插入 ID

映射。多对一和一对多的关系仅由一列表示。它是子(类别)表 TopicID 中的外键列。

所以这是正确的映射:
// Topic
<bag name="Categories" lazy="true" inverse="true"
                     batch-size="25" cascade="all-delete-orphan">
  // this is wrong
  // <key column="ID" />
  // this is the target column in the other table
  <key column="TopicID" />
  <one-to-many class="Category" />
</bag>
...
// Category
// the same columns for the same relation
<many-to-one name="Topic" class="Topic" column="TopicID" />

有了级联,我们可以保存主题:

session.Update(t);
// not needed, there is a cascade
// session.Save(c);

我认为你的实体不完整,你应该修改你的代码。不要忘记对IDisposable对象使用using语句。

using(ISession session = dal.GetSession())
{
using (session.BeginTransaction())
{
    Topic t = session.Get<Topic>(topicId);
    Category c = new Category() { Name=name };
    t.AddCategory(c);
    c.Topic = t;
    session.SaveOrUpdate(t);
    session.Transaction.Commit();
}
}

你也可以在t.AddCategory(c);方法中:

public void AddCategory(Category category)
{
  // add to list
  category.Topic = this;
}