简体   繁体   中英

NHibernate many-to-one mapping

Entity classes:

public class Purchase
{
   public virtual int PurchaseId { get; set; }
   public virtual int CustomerId { get; set; }
   public virtual Customer Customer { get; set;}
}

public class Customer
{
    public virtual int CustomerId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

hibernate configs:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="xxx" assembly="xxx">
  <class name="Purchase" table="Purchase">
    <id name="PurchaseId" column="PurchaseId" />
    <many-to-one name="Customer" column="CustomerId"/>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="xxx" assembly="xxx">
  <class name="Customer" table="Customer">
    <id name="CustomerId" column="CustomerId" />
    <property name="FirstName" column="FirstName" />
    <property name="LastName" column="LastName" />
  </class>
</hibernate-mapping>

I am trying to save new purchase to database:

 using (var session = _sessionFactory.Create())
 {
     var customer = new Customer { CustomerId = 1, FirstName = "John", LastName = "Smith" };
     var purchase = new Purchase { PurchaseId = 1, Customer = customer };

     session.Save(purchase);
     session.Flush();
 }

Purchase table have FK to Customer table.

And I am getting: "SqlException: Cannot insert the value NULL into column 'CustomerId', table 'ORM.dbo.Purchase'; column does not allow nulls. INSERT fails."

I am wondering, what I am doing wrong. Thank you very much for anserw.

The problem might be that NHibernate does not really know what value of CustomerId to insert into the Purchase table, because it does not know how to obtain that key.

Could you please try and change the mapping to include a generator

Please note that according to NHibernate Docs

generator: The name of a generator class to use for generating unique identifiers for instances of the persistent class. See the generator element. Specifying either the attribute or the element is required. The attribute takes precedence over the element.

Now, which exact generator to pick, is a bigger topic that depends on your use cases, see some info in How to auto generate IDs in NHibernate

If you would like to assign keys in code, you should pick the assigned generator, see below

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="xxx" assembly="xxx">
  <class name="Purchase" table="Purchase">
    <id name="PurchaseId" column="PurchaseId" generator="assigned" />
    <many-to-one name="Customer" column="CustomerId"/>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="xxx" assembly="xxx">
  <class name="Customer" table="Customer">
    <id name="CustomerId" column="CustomerId" generator="assigned"/>
    <property name="FirstName" column="FirstName" />
    <property name="LastName" column="LastName" />
  </class>
</hibernate-mapping>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM