1

我遇到了以下异常,我很确定这与类别和产品之间的一对多关系有关。怎么了?

Nhibernate.Exceptions.GenericADOException {“无法执行批处理命令。[SQL:SQL 不可用]”}

InnerException: {"INSERT 语句与 FOREIGN KEY 约束 \"FK1F94D86A1B57B09E\" 冲突。冲突发生在数据库 \"SimpleNhibernate\"、表 \"dbo.Category\"、列 'Id' 中。\r\n语句有已终止。"}

程序.cs

class Program
{
    private static ISessionFactory _sessionFactory;
    private static Configuration _configuration;

    static void Main(string[] args)
    {

        _configuration = new Configuration();
        _configuration.Configure();
        _configuration.AddAssembly(typeof(Product).Assembly);
        _sessionFactory = _configuration.BuildSessionFactory();

        var schema = new SchemaExport(_configuration);
        schema.Drop(true, true);
        schema.Create(true, true);

        AddCategory(new Category("Phone"));
        AddCategory(new Category("Smart Phone"));

        using (ISession session = NHibernateHelper.OpenSession())
        {
            var categories = session.QueryOver<Category>().List();                
            foreach (var category in categories)
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(new Product("iPhone", category));      
                    transaction.Commit(); 


                }
            }
        }

    }

    private static void AddCategory(Category product)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            session.Save(product);
            transaction.Commit();
        }
    }
}

类别.cs

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iesi.Collections.Generic;
namespace SimpleNhibernate
{
    public class Category
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual ISet<Product> Products { get; set; }
        public virtual bool Discontinued { get; set; }

        public Category() { }

        public Category(string name)
        {
            Name = name;
        }
    }
}

类别.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="SimpleNhibernate"
                   namespace="SimpleNhibernate">

  <class name="Category">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />
    <set name="Products" inverse="true" >
      <key column="Id" />
      <one-to-many class="Product"/>
    </set>
    <property name="Discontinued" />
  </class>

</hibernate-mapping>

产品.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   assembly="SimpleNhibernate" 
                   namespace="SimpleNhibernate">

  <class name="Product">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />
    <many-to-one name="Category" column="CategoryID"></many-to-one>
    <property name="Discontinued" />
  </class>

</hibernate-mapping>

产品.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleNhibernate
{
    public class Product
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Category Category { get; set; }
        public virtual bool Discontinued { get; set; }

        public Product() { }
        public Product(string name, Category category)
        {
            Name = name;
            Category = category;
        }
    }
}
4

1 回答 1

2

负责映射(双向)的列CategoryID产品表中

所以set应该有这样的钥匙

 <class name="Category">
    ...
    <set name="Products" inverse="true" >
      <key column="CategoryID" /> // instead of Id

与产品映射中的相同

<many-to-one name="Category" column="CategoryID"></many-to-one>
于 2013-02-22T08:23:27.183 回答