0

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.

Piotr X
  • 107
  • 1
  • 9
  • Hi, what kind of generator are you using for the key values? I can see none in the config you've posted. The docs say it's required. The problem might be that NHibernate inserts customer first, and then, does not know how to get a key value for it. https://nhibernate.info/doc/nhibernate-reference/mapping.html – ironstone13 Apr 03 '20 at 12:59
  • Let me know, if my answer does not solve your problem, I can look into this in more detail. Best of luck! – ironstone13 Apr 03 '20 at 13:14
  • Unfortunetally, I have to generate keys from code, not by Db. So I probably need other solution. Thank you so much for your time. – Piotr X Apr 03 '20 at 13:15
  • In that case, please use the `generator=assigned`, I will update my answer – ironstone13 Apr 03 '20 at 13:17
  • It is working fine, thank you so much! – Piotr X Apr 03 '20 at 13:26
  • You are most welcome! – ironstone13 Apr 03 '20 at 13:30

1 Answers1

1

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>
ironstone13
  • 3,325
  • 18
  • 24