-->
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.  [ 4 posts ] 
Author Message
 Post subject: Help required to make code more efficient!..
PostPosted: Sat Oct 16, 2004 11:21 am 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
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


Top
 Profile  
 
 Post subject: Child items not being properly updated!!!
PostPosted: Sat Oct 16, 2004 4:38 pm 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
I have made the following amendments to the previous code:

newStk = stk.checkStock(stockID);

item.setStock(newStk);
item.setAmount(amount);
item.setPrice(newStk.getUnitCost() * amount);
item.setArtistName(artist_name);

neworder.setPriceTotal(neworder.getPriceTotal() + (newStk.getUnitCost() * amount))
neworder.setUserID(parseInt(user.getID()));

neworder.setOrderItems(new HashSet());
neworder.getOrderItems().add(item);
item.setOrder(neworder);

The problem I am having is that each subsequent child item being added is overwriting the previous item in the list and is not being added to the item list, so it seems that there is only ever one child item associated with the parent. What am i missing?

regards


Andrew


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:47 pm 
Newbie

Joined: Sat Aug 30, 2003 12:37 pm
Posts: 16
Please read this : http://www.hibernate.org/109.html

It will solve you problem.


Top
 Profile  
 
 Post subject: Ah.....
PostPosted: Sun Oct 17, 2004 2:43 am 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
OK!,
oh further investigation I came across this in chapter 7 of the hibernate manual (Component Mapping):

7.4 Components as composite identifiers
............
'You may instead implement Interceptor.isUnsaved() if you wish to use saveOrUpdate() or cascading save / update. As an alternative, you may also set the unsaved-value attribute on a <version> (or <timestamp>) element to specify a value that indicates a new transient instance. In this case, the version of the entity is used instead of the (assigned) identifier and you don't have to implement Interceptor.isUnsaved() yourself.'

So, can anybody tell me how to implement

1. Interceptor.isUnsaved()?
2. Set the unsaved-value attribute on a <version> or timestamp?

I would like to be see examples for both if possible ;-)

many thanks in advance

Andrew


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