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>