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.)
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);
-
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?
0 2597 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rob |
last post by:
We are starting a new fairly big project based on the typical 3-tier
approach (db, business, GUI layer). I have limited experience in that field
but from my experience all the stored procedures,...
|
by: MyGeneration |
last post by:
MyGeneration is 100% free, tired of hand coding business objects, stored
procedures, why not spend your time doing other things? Let us help you.
See http://www.mygenerationsoftware.com
We also...
|
by: Nishanthan |
last post by:
Hi,
Is Anybody used nHibernate for your projects?
Thanks,
P.Nishanthan
|
by: FB's .NET Dev PC |
last post by:
I am writing two services in VB.NET, one of which needs to send text strings
to the other. After reading, I
decided (perhaps incorrectly) that named pipes would be the best
interprocess...
|
by: Hoffmania |
last post by:
Is there a good tutorial of getting databinding working with NHibernate
collections such as bags with lazy loading. I can access the collection
programaticly, but not directly with databinding.
|
by: Fregas |
last post by:
I'm the lead developer at my company and i'm looking into O/R Mappers
again. I've used LLBLGen and Wilson ORM and played with some others.
I prefer WORM because of its simplicity and the fact that...
|
by: tjb |
last post by:
I often see code like this:
/// <summary>
/// Removes a node.
/// </summary>
/// <param name="node">The node to remove.</param>
public void RemoveNode(Node node) {
<...>
}
|
by: Peter Morris [Droopy eyes software] |
last post by:
Hi all
Any recommendations for a good object persistence framework? I'd be
interested in hearing from anyone with personal experience.
Thanks
Pete
|
by: uod |
last post by:
how do you use composite keys to work on nhibernate?
|
by: Alexander Vasilevsky |
last post by:
Does the number NHibernate download data? For example, first can be
downloaded 100 rows of data, and then as necessary podgruzhat else.
Where can I read about NHibernate and its potential?
...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |