473,594 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NHibernate unidirectional one-to-one mapping problem.

13 New Member
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.xm l
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 2609

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

Similar topics

8
2719
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, com object, etc is quite cumbersome and error prone. Therefore, I am currently looking into some OR Mappers. I did a very simple sample project with Wilson's ORMapper. It seems to me ORMappers are exactly what I am looking for. In any case, I...
0
1108
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 offer a free .NET Architecture available in C# and VB.NET called dOOdads that you can generate in seconds, we also have Gentle.NET, NHibernate and other templates. With dOOdads you can point and click on a table and generate your stored...
0
975
by: Nishanthan | last post by:
Hi, Is Anybody used nHibernate for your projects? Thanks, P.Nishanthan
2
5052
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 communication method for this task. I set about creating, on the pipe's server side, a new thread which would instantiate a blocking inbound message pipe, and the client side which would write to that pipe when needed. Being on the same PC, security is...
1
2258
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
2120
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 its basicly open source, but it has some limitations and code generation/update is very limited. I liked LLBLGen's but found using it kind of complex. Does anyone have any other suggestions? Is there something new out there? Some features I...
98
4546
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
2940
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
2304
by: uod | last post by:
how do you use composite keys to work on nhibernate?
1
1707
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? http://www.alvas.net - Audio tools for C# and VB.Net developers
0
8259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8244
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6669
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3871
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3905
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1487
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.