-->
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.  [ 3 posts ] 
Author Message
 Post subject: I need help - many-to-many
PostPosted: Sat Aug 19, 2006 7:44 pm 
Newbie

Joined: Sat Aug 19, 2006 7:19 pm
Posts: 2
Hello I have serious problem and I can't find solution.
My problem is in mapping this example:
----------------------------------------------------
//Class Order reperesent basic order, can containts more OrderItem
class Order {
private int id_o;
private Set<OrderItem> orderItems;
...
}

//class reprezent oreder item and can contain only one product and its //quantity
class OrderItem {
private int quantity;
private Product product;
}

//class represent basic product
class Product {
private int id_p;
private String name;
.....
}
I would like to map it into join table , but i don't know how should map atrribute quantity.
I know that I should use many-to-many mapping, but i know only use it when I don't have attribute quantity.

ERD:(Tables)

ORDER
------------
ID_O integer auto_increment PK

PRODUCT
-----------
ID_P integer auto_increment PK
NAME varchar(20)

ORDER_ITEM
-----------------
ID_O PK,FK
ID_P PK,FK
QUANTITY integer

But how should it map in hibernate, I really don't know.
If someone know, please help me.
Thanks for your help
geN.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 20, 2006 6:14 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You probably don't want a many-to-many here. You could do it with composite-element, but a better (IMO) solution is to copy the Customer/Order/Product example in section 23.3 of the ref docs. The LineItem table in that example exactly matches your OrderItem table. Just convert the <list> in the example to the <set> that you're using, and it should work perfectly.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: I solve this problem
PostPosted: Sun Aug 20, 2006 6:30 pm 
Newbie

Joined: Sat Aug 19, 2006 7:19 pm
Posts: 2
Hey, You have right that I don't need many-to-many-mapping.
I use this mapping in Order.hbm.xml

....
<set name="orderItems" table="ORDER_ITEM">
<key column="ID_O" />
<composite-element class="package.OrderItem">
<many-to-one name="product" class="package.Product">
<column name="ID_P" />
</many-to-one>
<property name="quantity" column="QUANTITY" />
</composite-element>
</set>

...

It works correcty, but maybe the solution that you've written is better, I will check it.

Thanks a lot

.geN


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.