Hi,
I am facing problem with many-to-one relation. I am unable to map the key column with non-primary key in child table. I worked on a sample application with sqlserver2005 as database and VS2008 .
My sample is ....
I have 2 tables "Customer" and "Order" tables.
Customer table columns : CustomerID,Name,City,State,CustomerCode(unique key)
Order table columns : OrderID,Date,CustomerID
my mapping for list of orders as following in "customer.hbm.xml"
Code:
<!-- One-to-many mapping: Orders -->
<set name="Orders" cascade="all-delete-orphan" lazy="false">
<<key column="CustomerID" />
<one-to-many class="Order" />
</set>
"many-to-one" mapping in "Orders.hbm.xml" as following..
Code:
<!-- Many-to-one mapping: Customer -->
<many-to-one name="Customer"
class="Customer"
column="CustomerID"
cascade="all" />
The above mapping is working fine with the association between 2 tables with "customerId".In customer entity object i get the associated "Orders" list and in "Order" entity object i get the associated customer object as a customer object.
But i want to map the relations using
non-primary key in "customer" table.
I added new column "CCode" and removed "CutomerId" column from "Order" table.
Now i want to get the association results with the relations with this "CutomerCode" from cutomer object. I made some changes in mapping file.In order.hbm.xml i added property-ref="CustomerCode" to <many-to-one tag. Then i get "Order" object and also i am getting the associated "Customer" object for the "order" using "CCode"
But when i check the "Customer" object it is not giving the associated "orders" list for the customer object.
Note: In customer table "customercode" is not a primary key. It is a non-primary key.If i mapped with "CustomerId" data is loading.
I want some help on this mapping.
1. Is it possible to map the data with non-primary key.
2. If possible what is wrong in the above mapping code.How can we mapping tables with non-primary key?
Any help can be appreciated.
Thanks & Regards,
Kiran