473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

NHibernate unidirectional one-to-one mapping problem.

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

Example: An Order is given by a Customer.

Expand|Select|Wrap|Line Numbers
  1. Customer{ID, Name, Address}
  2. 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
Expand|Select|Wrap|Line Numbers
  1. ------------
  2. CREATE TABLE [dbo].[Customer](
  3.     [ID] [int] IDENTITY(1,1) NOT NULL,
  4.     [Name] [varchar](50) NULL,
  5.     [Address] [varchar](50) NULL,
  6.  CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
  7. (
  8.     [ID] ASC
  9. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  10. ) ON [PRIMARY]
Customer.cs
Expand|Select|Wrap|Line Numbers
  1. -----------
  2. public class Customer
  3. {
  4.     private int _id;
  5.     public virtual int ID
  6.     {
  7.         get { return _id; }
  8.         set { _id = value; }
  9.     }
  10.  
  11.     private string _name;
  12.     public virtual string Name
  13.     {
  14.         get { return _name; }
  15.         set { _name = value; }
  16.     }
  17.  
  18.     private string _address;
  19.     public virtual string Address
  20.     {
  21.         get { return _address; }
  22.         set { _address = value; }
  23.     }
  24. }
  25.  
Customer.hbm.xml
Expand|Select|Wrap|Line Numbers
  1. ----------------
  2. <?xml version="1.0" encoding="utf-8" ?>
  3.  
  4. <hibernate-mapping
  5.   xmlns="urn:nhibernate-mapping-2.2"
  6.   >
  7.   <class name="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO" table="Customer">
  8.     <id name="ID" >
  9.       <generator class="native" />
  10.     </id>
  11.     <property name="Name" column="Name" />      
  12.     <property name="Address" column="Address" />
  13.   </class>
  14. </hibernate-mapping>
OrderN.sql
Expand|Select|Wrap|Line Numbers
  1. ---------
  2. CREATE TABLE [dbo].[OrderN](
  3.     [ID] [int] IDENTITY(1,1) NOT NULL,
  4.     [Customer] [int] NULL,
  5.     [OrderDate] [datetime] NULL,
  6.  CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED 
  7. (
  8.     [ID] ASC
  9. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  10. ) ON [PRIMARY]
  11.  
  12. GO
  13. ALTER TABLE [dbo].[OrderN]  WITH CHECK ADD  CONSTRAINT [FK_Order_Customer] FOREIGN KEY([Customer])
  14. REFERENCES [dbo].[Customer] ([ID])
  15. GO
  16. ALTER TABLE [dbo].[OrderN] CHECK CONSTRAINT [FK_Order_Customer]
OrderN.cs
Expand|Select|Wrap|Line Numbers
  1. ---------
  2. public class OrderN
  3. {
  4.     private int _id;
  5.     public virtual int ID
  6.     {
  7.         get { return _id; }
  8.         set { _id = value; }
  9.     }
  10.  
  11.     private Customer _customer;
  12.     public virtual Customer Customer
  13.     {
  14.         get { return _customer; }
  15.         set { _customer = value; }
  16.     }
  17.  
  18.     private DateTime _orderDate;
  19.     public virtual DateTime OrderDate
  20.     {
  21.         get { return _orderDate; }
  22.         set { _orderDate = value; }
  23.     }
  24. }
  25.  
OrderN.hbm.xml
Expand|Select|Wrap|Line Numbers
  1. --------------
  2.  
  3. <?xml version="1.0" encoding="utf-8" ?>
  4.  
  5. <hibernate-mapping 
  6.     xmlns="urn:nhibernate-mapping-2.2" 
  7.     >
  8.   <class name="NHibernate__One_To_One__Order_Customer.BO.OrderN, NHibernate__One_To_One__Order_Customer.BO" table="OrderN">
  9.     <id name="ID">      
  10.       <generator class="native" />
  11.     </id>
  12.     <property name="OrderDate" column="OrderDate"/>
  13.  
  14.  
  15.     <one-to-one
  16.         name="Customer"
  17.         class="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO" />
  18.  
  19.  
  20.   </class>
  21. </hibernate-mapping>

Main-program
Expand|Select|Wrap|Line Numbers
  1. ------------
  2. OrderN o = new OrderN();
  3. o.OrderDate = DateTime.Now;
  4. o.Customer = new Repository<Customer>().Get<Customer>(1);
  5.  
  6. Repository<OrderN> rep = new Repository<OrderN>();
  7. rep.Save(o);
  8.  
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?
Jan 25 '10 #1
0 2597

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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,...
0
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...
0
by: Nishanthan | last post by:
Hi, Is Anybody used nHibernate for your projects? Thanks, P.Nishanthan
2
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...
1
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.
17
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...
98
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) { <...> }
13
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
2
by: uod | last post by:
how do you use composite keys to work on nhibernate?
1
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? ...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
0
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...
0
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...
1
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....
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.