开发者

NHibernate unidirectional one-to-one mapping problem

开发者 https://www.devze.com 2022-12-18 05:42 出处:网络
I am trying to create a Unidirectional one-to-one relationship using NHibernate. Example: An Order is given by a Customer.

I am trying to create a Unidirectional one-to-one relationship using NHibernate.

Example: An Order is given by a Customer.

Customer{ID, Name, Address}
OrderN{ID, Customer, OrderDate}

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.)

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.

And if I add <property name="Customer" column="Customer" /> in OrderN.hbm.xml, an exception is thrown:

Could not determine type for: NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO, 开发者_StackOverflowVersion=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Customer)

How to solve this problem?

May be it is not a one-to-one relationship. I am actually trying to understand how is the <one-to-one /> tag used. Can anyone please help me in this regard?

Customer.sql

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]

Customer.cs

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; }
    }

}

Customer.hbm.xml

<?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>

OrderN.sql

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]

OrderN.cs

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; }
    }
}

OrderN.hbm.xml

<?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

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);


So a Customer can only ever have one Order? I hope it's a big one!

This isn't a one-to-one relationship, it's one-to-many. Customer is the one side, and Order is the many (Customer has Orders). Try mapping it like this:

<?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"/>


    <many-to-one
        name="Customer"
        class="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO"
        column="Customer" />


  </class>
</hibernate-mapping>
0

精彩评论

暂无评论...
验证码 换一张
取 消