-->
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: Many-to-many not making me happy!
PostPosted: Mon May 30, 2005 9:33 am 
Newbie

Joined: Sat Apr 23, 2005 10:50 pm
Posts: 17
I have a Menu class that keeps a collection of SalesLineItem objects.
Basically the Menu is a food menu, such as a pizza menu, and the
items such as Coke or Margeritha Pizza are SalesLineItem objects.
The menu lists all the SalesLineItem's it has, depending on what menu the
user chooses.
It may be an Italian food menu or chinese food menu.
This makes the relationship between the Menu class and the SalesLineItem
class a many-to-many relationship.
Also note that the SalesLineItem objects stored in a menu all
have properties such as name, itemcode, price and quantity.

Now, when the user of the system creates a sale, the application creates
a Sale object which also has a many-to-many relationship with the
SalesLineItem class.

First the application retrieves the correct menu, say for example, the
Italian menu.
Then, given an item code we can retrieve an item from the menu and add
it to the sale.

SalesLineItem sli = (SalesLineItem)menu.getSaleslineitems().get(code);
Sale sale = new Sale();
sale.getSaleslineitems().put(sli.getCode(),sli);

This all works fine and is persisted.
But if I increase the quantity, or make any changes dynamically to
a SalesLineItem object after retrieving it from the menu and put it in the
Sale object, these changes happens to any object that keeps a reference
to the changing object.

For example I retrieve a SalesLineItem object such as a Coke from the
Menu class.
I increase the quantity of the Coke , because the customer wants two
cokes for his order, and then add the Coke to a new Sale object.
Now the Coke has a quantity of 2 in the menu, in the new sale
and as well as in other sales that has a coke.
That is the problem, what is the solution?, misuse of hibernate or
must find workarounds for this problem.
Thanks!

See my mapping file here below , if you can spot any errors please!

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="com.ktechvision.model.menu.Menu"
    table="menu">
   <id name="id" unsaved-value="0">
      <generator class="native"/>
   </id>
   <map name="saleslineitems"
                  table="menu_saleslineitem" cascade="all">
    <key column="parent_id"/>
                 <index column="saleslineitem_name" type="string"/>
    <many-to-many column="saleslineitem_id"      class="com.ktechvision.model.menu.SalesLineItem"/>
   </map>
   
   <property name="name" type="string"/>
   <property name="code" type="int"/>
</class>

<class name="com.ktechvision.model.menu.SalesLineItem"
    table="saleslineitem">
   <id name="id" unsaved-value="0">
      <generator class="native"/>
   </id>
   
   <property name="name" type="string"/>
   <property name="description" type="string"/>
   <property name="price" type="double"/>
   <property name="code" type="string"/>
   <property name="qty" type="int"/>
   <property name="total" type="double"/>
</class>



<class name="com.ktechvision.model.sales.Sale"
    table="sale">
   <id name="id" unsaved-value="0">
      <generator class="native"/>
   </id>
   
                <map name="saleslineitems"
     table="sale_saleslineitem" cascade="all">
     <key column="parent_id"/>
     <index column="saleslineitem_name" type="string"/>
     <many-to-many column="saleslineitem_id" class="com.ktechvision.model.menu.SalesLineItem"/>
    </map>
   
               <property name="date" type="date"/>
               <property name="type" type="string"/>
               <property name="total" type="double"/>
</class>   

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 11:41 am 
Regular
Regular

Joined: Thu Apr 15, 2004 1:12 pm
Posts: 55
Hi Sinoea,

Your problem isn't with Hibernate, or many-to-one, or your mapping. It's with your object model!

You need a MenuItem class. Right now you are trying to make your SaleLineItem class also function as a MenuItem. That'll never work, for the reasons you are experiencing!

Here's what you want...

A Menu has a Set (many-to-many) of MenuItems.

A MenuItem has properties: name, price etc. (but note: not[i] qty or total!)

A Sale has a Set (one-to-many) of SaleLineItems.

A SaleLineItem has:
• a menuItem property (many-to-one to MenuItem)
• some value-type properties: qty, subtotal

Does that clear things up?
—ml—



Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 11:14 pm 
Newbie

Joined: Sat Apr 23, 2005 10:50 pm
Posts: 17
ml wrote:
Hi Sinoea,

Your problem isn't with Hibernate, or many-to-one, or your mapping. It's with your object model!

You need a MenuItem class. Right now you are trying to make your SaleLineItem class also function as a MenuItem. That'll never work, for the reasons you are experiencing!

Here's what you want...

A Menu has a Set (many-to-many) of MenuItems.

A MenuItem has properties: name, price etc. (but note: not[i] qty or total!)

A Sale has a Set (one-to-many) of SaleLineItems.

A SaleLineItem has:
• a menuItem property (many-to-one to MenuItem)
• some value-type properties: qty, subtotal

Does that clear things up?
—ml—


Billion trillion thanks, it works fantastic now!


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.