-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with mapping entity relations
PostPosted: Wed Nov 04, 2009 7:21 am 
Newbie

Joined: Sat Oct 31, 2009 9:38 am
Posts: 5
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.