-->
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.  [ 7 posts ] 
Author Message
 Post subject: a little different many-to-many relationship question
PostPosted: Wed Feb 11, 2004 2:43 pm 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
Hello, I'm kind of new to hibernate and I was trying basics examples with no real problem so then I did a little database on my own but there is a thing I want to do that I've never seen explained nowhere and that might be possible but my level doesn't allow me to find for now.

Here is the general relation table :
[img]
http://nerae.free.fr/general-model.jpg
[/img]
And the UML model :
[img]
http://nerae.free.fr/uml-model.jpg
[/img]

Here are my classes :

Product.java :
Quote:
public class Product {

private int id;
private String description;
private double prize;

public Product() {
}

....
Standards get and set methods
....

}
}


Product.hbm.xml
Quote:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="Product" table="PRODUCTS">
<id name="id" type="int" unsaved-value="null">
<column name="id" sql-type="int" not-null="true" />
<generator class="uuid.hex" />
</id>
<property name="description"/>
<property name="prize"/>
</class>
</hibernate-mapping>


Order.java
Quote:
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;

public class Order {
private String id;
private Date order_date;
private double final_cost;
private boolean order_confirmed;
private Set product_ordered = new HashSet();

public Order() {
}

....
Standards get and set methods
....

public Set getProduct_ordered(){
return product_ordered;
}

public void setProduct_ordered(Set product_ordered){
this.product_ordered=product_ordered;
}
}


Order.hbm.xml :
Quote:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>

<class name="Order" table="ORDERS">
<id name="id" type="string" unsaved-value="null">
<column name="id" sql-type="char(32)" not-null="true" />
<generator class="uuid.hex" />
</id>
<property name="order_date" type="timestamp"/>
<property name="final_cost"/>
<property name="order_confirmed"/>
<set name="product_ordered" table="product_ordered">
<key column="orders_id"/>
<many-to-many column="product_id" class="Product"/>
</set>
</class>
</hibernate-mapping>


But then I need to add the quantity element in this relation but I don't know How to do ^^;
May I have to create a class for product_id and define two many-to-one relations ? Didn't it exist a mean to add an elemnt or property into the set declaration ? Maybe create a class wich is an extend of a Set and can implement two element a the same time ? or store the both product_id and quatity value associated in some kinf of vector object ? but then how to map it into hibernate ? ^^;

Please if someone has a solution for this problem I will be very glad.
Thanks.
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 4:31 pm 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
Hu ? Why can't I edit my own post ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 5:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well you have answered your question yourself allready basically with your UML diagram. Model it using an intermediate class with two many-to-one mappings.

PS: We don't have edit functionality enabled on this board.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 5:45 pm 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
Erf I thought that maybe it would have been possible to use a customised Class extended from a Set object that will allow me to set and get the product as the quantity :\
Thanks anyway ^^

PS : too bad that there is no mean to edit our own posts mine was full of errors if I'd know I would have previsualised before ^^;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 5:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could probably try a map, with an index-many-to-many mapped to your Product class and the index maped to your ammount. Could probably work like this (inside of Order):

Code:
<map name="products" table="order_product">
      <key column="order_id"/>
      <index-many-to-many column="product_id" class="Product"/>
      <element column="ammount" type="int"/>
</map>

Just off my head now, don't know if it works that way.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 5:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Ehm I meant element maped to your ammount.

So much for the edit functionality :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 9:00 pm 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
YES !
Thanks a lot gloeglm I didn't know i could do that with a Map :D ^
I didn't undertood that the put method of the map was like an add to the set but with an associated value and more of that it is more efficient that a Set in this case.
I was sure it was a mean, I'm glad you were here to show me ^^


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