Hi,
I am new to this Hibernate situation so please bare with me ;-). I have created a simple shopping cart system which at the moment only adds order items to an order. I have now managed to get myself completely stuck when it comes to writing the code which deletes an item from the order before the order is comitted to the database as I do not know how to access the order item ID. Is an order item ID generated before the order is commited, if so how can I access it? Also, is there a more efficient way of adding an order item to an Order?
Version[b/] 2.1.1
My maps are as follows:
[b]Order.hbm.xml[b/] <hibernate-mapping package="test"> <class name="Order" table="orderstbl"> <id name="ID" column="order_id" type="string" unsaved-value="null"> <generator class="uuid.hex"/> </id> <property name="OrderDate" column="order_date" type="timestamp" not-null="true"/> <property name="PriceTotal" column="price_total" type="double" not-null="true"/> <property name="UserID" column="user_id" type="integer" not-null="true"/> <set name="OrderItems" table="orderitemstbl" inverse="true" cascade="all"> <key column="order_id"/> <one-to-many class="OrderItem"/> </set> </class> </hibernate-mapping>
[b]OrderItem.hbm.xml[b/] <hibernate-mapping package="test"> <class name="OrderItem" table="orderitemstbl"> <id name="ID" column="orderitem_id" type="string" unsaved-value="null"> <generator class="uuid.hex"/> </id> <property name="OrderID" column="order_id" type="string" not-null="true" insert="false" update="false"/> <property name="StockID" column="stock_id" type="string" not-null="true" insert="false" update="false"/> <property name="Amount" column="amount" type="integer"/> <property name="Price" column="price" type="double"/> <many-to-one name="Order" class="Order" column="order_id" /> <many-to-one name="Stock" class="Stock" column="stock_id"/> </class> </hibernate-mapping>
[b]Stock.hbm.xml[b/] <hibernate-mapping package="test"> <class name="Stock" table="stocktbl"> <id name="ID" column="stock_id" type="string" unsaved-value="null"> <generator class="uuid.hex"/> </id> <property name="ArtistID" column="artist_id" type="integer" not-null="true"/> <property name="ItemType" column="item_type" type="string" not-null="true"/> <property name="ItemTitle" column="item_title" type="string"/> <property name="ItemDemo" column="item_demo_location" type="string"/> <property name="ItemFinal" column="item_final_location" type="string"/> <property name="UnitCost" column="unit_cost" type="double"/> <property name="ItemSelected" column="item_selected" type="boolean"/> <property name="ItemPurchaseAmount" column="item_purchase_amount" type="integer"/> <one-to-one name="StockImages" outer-join="false" class="StockImages" cascade="delete"/> </class> </hibernate-mapping>
The following Class code that adds an item:
[b]Order[b/] public void addProduct(Stock s, int amount, int user_ID) { OrderItem orderItem = new OrderItem(this, s, amount); this.PriceTotal = this.PriceTotal + s.getUnitCost() * amount; this.UserID = user_ID; this.OrderItems.add(orderItem); }
[b]OrderItem[b/] public OrderItem(Order order, Stock stock, int amount) { this.Order = order; this.Stock = stock; this.Amount = amount; this.Price = stock.getUnitCost() * amount; Artist getartist = Artist.getArtist(Stock.getArtistID().intValue()); this.ArtistName = getartist.getArtistName(); }
The code I use to save the order is: [b]checkstock[b/] transaction = session.beginTransaction(); //Get Artist details stockItem = (Stock)session.load(Stock.class, item_code); transaction.commit(); HibernateUtil.closeSession();
[b]client code[b/] //1. Check if stock code exists. newStk = stk.checkStock(cocoon.request.get("stockID")); //2. Add a stock item to the order. neworder.addProduct(newStk,cocoon.request.get("quantity"),user.getID());
[b]server[b/] transaction = session.beginTransaction();
session.save(myOrder); transaction.commit(); Util.closeSession();
many thanks in advance
Uzo
|