I am trying to create a Unidirectional one-to-one relationship using NHibernate.
Example: An Order is given by a Customer.
Here, OrderN.Customer-field is intended to store Customer.ID as an FK. And this field doesn't have any unique constraint.
(The OrderN-table is given such a name to avoid SQL keyword conflict.)
Customer.sql
Customer.cs
Customer.hbm.xm l
OrderN.sql
OrderN.cs
OrderN.hbm.xml
Main-program
The problem is, After executing this c# code, OrderN.Customer-field is storing a null value.
But it was supposed to store the ID of the Customer. I.e. 1.
How to solve this problem?
Example: An Order is given by a Customer.
Code:
Customer{ID, Name, Address}
OrderN{ID, Customer, OrderDate}
(The OrderN-table is given such a name to avoid SQL keyword conflict.)
Customer.sql
Code:
------------ CREATE TABLE [dbo].[Customer]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL, [Address] [varchar](50) NULL, CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Code:
-----------
public class Customer
{
private int _id;
public virtual int ID
{
get { return _id; }
set { _id = value; }
}
private string _name;
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
private string _address;
public virtual string Address
{
get { return _address; }
set { _address = value; }
}
}
Code:
----------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
>
<class name="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO" table="Customer">
<id name="ID" >
<generator class="native" />
</id>
<property name="Name" column="Name" />
<property name="Address" column="Address" />
</class>
</hibernate-mapping>
Code:
--------- CREATE TABLE [dbo].[OrderN]( [ID] [int] IDENTITY(1,1) NOT NULL, [Customer] [int] NULL, [OrderDate] [datetime] NULL, CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[OrderN] WITH CHECK ADD CONSTRAINT [FK_Order_Customer] FOREIGN KEY([Customer]) REFERENCES [dbo].[Customer] ([ID]) GO ALTER TABLE [dbo].[OrderN] CHECK CONSTRAINT [FK_Order_Customer]
Code:
---------
public class OrderN
{
private int _id;
public virtual int ID
{
get { return _id; }
set { _id = value; }
}
private Customer _customer;
public virtual Customer Customer
{
get { return _customer; }
set { _customer = value; }
}
private DateTime _orderDate;
public virtual DateTime OrderDate
{
get { return _orderDate; }
set { _orderDate = value; }
}
}
Code:
--------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
>
<class name="NHibernate__One_To_One__Order_Customer.BO.OrderN, NHibernate__One_To_One__Order_Customer.BO" table="OrderN">
<id name="ID">
<generator class="native" />
</id>
<property name="OrderDate" column="OrderDate"/>
<one-to-one
name="Customer"
class="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO" />
</class>
</hibernate-mapping>
Main-program
Code:
------------ OrderN o = new OrderN(); o.OrderDate = DateTime.Now; o.Customer = new Repository<Customer>().Get<Customer>(1); Repository<OrderN> rep = new Repository<OrderN>(); rep.Save(o);
But it was supposed to store the ID of the Customer. I.e. 1.
How to solve this problem?