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.  [ 6 posts ] 
Author Message
 Post subject: one-to-one or many-to-many mapping problem
PostPosted: Sat Oct 11, 2008 4:01 pm 
Newbie

Joined: Sat Oct 11, 2008 3:49 pm
Posts: 5
I have a database setup like this:

Code:
Product:
    Product_Id

Product_Color:
    Product_Color_Id
    Product_Id
    Color_Id

Color:
    Color_Id

Order_Detail:
    Product_Id
    Product_Color_Id


So the Product is a many-to-many with Color. An Order_Detail has a Product and the Color that was chosen for the Product. So, Order_Detail has a one-to-one with Product_Color which has a one-to-one with Color.

How can I map an Order_Detail directly to Color? Basically Order_Detail is a one-to-one with Color, but it goes through the Product_Color table. I would have put the Color_Id directly on Order_Detail, but I didn't design this database.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 2:20 pm 
Newbie

Joined: Sat Oct 11, 2008 3:49 pm
Posts: 5
Does anyone know how this can be done?

I'm new to NHibernate, but I would assume this is a fairly simple thing to do. I've been searching around and can't find anything like this situation.

I've seen cases in databases where the one-to-one link to another table had two joining tables in between it, so this should be a fairly common situation.

Any help is greatly appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 9:43 am 
Newbie

Joined: Mon Aug 29, 2005 5:21 am
Posts: 9
Hi narshe,
first thing: Product_Id is redundant in the Order_Detail table. Product_Color_Id seems to be the PK and therefore you just need a reference to that key. From there you can go to the Product and Color.

However you still need the many-to-many connection you asked for.

The following is done in my app with a table A, a table B and a table A_B which connects A and B as many-to-many. The table A and table B represent the objects A and B I use in the application. I don't have any direct access to the table A_B in the app.

The class A has a generic list of type B and the mapping for A has the following entry:

<bag name="B" table="A_B" cascade="all-delete-orphan" inverse="false">
<key>
<column name= "`A_ID`" />
</key>
<many-to-many class="B" fetch="join" >
<column name= "`B_ID`" />
</many-to-many>
</bag>

This should work for you as well.

Best,
Hilmar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 12:09 pm 
Newbie

Joined: Sat Oct 11, 2008 3:49 pm
Posts: 5
Hi silveraxe, thanks for the reply.

Quote:
first thing: Product_Id is redundant in the Order_Detail table. Product_Color_Id seems to be the PK and therefore you just need a reference to that key. From there you can go to the Product and Color.


Yes, I know. Unfortunately I inherited this database and can't change the structure.

Quote:
The class A has a generic list of type B and the mapping for A has the following entry:


In my case, I have table A, B, and A_B where A is a one-to-one with A_B, and A_B is a one-to-one with with B. So with my POCO I need to have A.B and NOT A.IList<B>, because there is always only one B. I have some many-to-many references, but this is actually a one-to-one reference that goes through another table, which I haven't seen any examples for.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2008 4:52 pm 
Newbie

Joined: Fri Jan 11, 2008 6:32 pm
Posts: 2
So you have
Table A
Table B
Table a_b (join table for the many-to-many between a and b)

based on your earlier point where you said that a order detail you want to map a many-to-one but unfortunately it does not sound like you can change the DB to make it a many-to-one instead of a many-to-many. What you can do is map it like it was stated in the example above and map it to the list as a private property, based on what you are saying it will never be more then one item. Create the single property you want (color) and change the getters and setters for this property to work with the first item from the list you have. You can set the cascade to cascade none, since you will not need to update any of the relationships.

What you really should do is change the db and just use the product id and the color id, or to just use the product_color_id and map from it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 2:35 pm 
Newbie

Joined: Sat Oct 11, 2008 3:49 pm
Posts: 5
I tried to do the many-to-many mapping with a private collection and a public property that gets the one instance of the collection. The mapping doesn't work though, because the key I'm using from table A to connect to A_B isn't the primary key, it's a foreign key.

What I have done for now is a many-to-one from A to A_B and many-to-one from A_B to B. I have to create an internal class for A_B to do this, but I can now have a public property A.B that accesses the private property A.A_B.B.

This works, but it would be nice to not have to create the dummy A_B class. It doesn't look like I'll be able to do this with the current database setup though.


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

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.