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.xm l - ----------------
-
<?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 2609 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, 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...
|
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...
|
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 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...
|
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 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...
|
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?
http://www.alvas.net - Audio tools for C# and VB.Net developers
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |