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.  [ 6 posts ] 
Author Message
 Post subject: Exception: a different object with the same identifier....
PostPosted: Thu Apr 15, 2004 12:15 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
value was already associated with the session: d1e22501fbd4255600fbd42559d30001, of class: test.Stock

Ok let me set the scene here, I have some simple session logic which checks to see if a session object has been created before. If it has not, an Order Object is created and an associated order item. If the session object has already been created, then further order items are created:

InsertOrder insertorder = new InsertOrder();

//1.Determine if an order has been created
Order newOrder = (Order)session.getAttribute("ORDER");

if (newOrder == null){
newOrder = new Order();
}

//2.check that stock exists
Stock newStk = InsertOrder.checkStock("d1e22501fbd4255600fbd42559d30001");

//3.Add stock item
newOrder.addProduct(newStk,1,32);

//4.Update Session object
session.setAttribute("ORDER", newOrder);

//5.Save stock item
//insertorder.newOrder(newOrder);

The problem I am having comes when I uncomment point 5, to insert the new order to the table. So effectively, as this is a test page, what I am doing is refreshing the page a few times to create an order and then some order items. i then uncomment point 5 and refresh the page again, at this point the error is generated. The debug code I am getting is:

04:33:23,379 DEBUG SessionImpl:528 - opened session
04:33:23,381 DEBUG JDBCTransaction:37 - begin
04:33:23,383 DEBUG JDBCTransaction:41 - current autocommit status:false
04:33:23,385 DEBUG SessionImpl:786 - saving [test.Order#d1e22501fbea3a9d00fbec39d139000e]
04:33:23,387 DEBUG Cascades:497 - processing cascades for: test.Order
04:33:23,389 DEBUG Cascades:506 - done processing cascades for: test.Order
04:33:23,391 DEBUG Cascades:497 - processing cascades for: test.Order
04:33:23,393 DEBUG Cascades:524 - cascading to collection: test.Order.OrderItems
04:33:23,394 DEBUG Cascades:113 - cascading to saveOrUpdate()
04:33:23,396 DEBUG SessionImpl:1321 - saveOrUpdate() unsaved instance
04:33:23,398 DEBUG SessionImpl:786 - saving [test.OrderItem#d1e22501fbea3a9d00fbec39d146000f]
04:33:23,400 DEBUG Cascades:497 - processing cascades for: test.OrderItem
04:33:23,402 DEBUG Cascades:113 - cascading to saveOrUpdate()
04:33:23,404 DEBUG Cascades:341 - id unsaved-value strategy NULL
04:33:23,406 DEBUG SessionImpl:1326 - saveOrUpdate() previously saved instance with id: d1e22501fbd4255600fbd42559d30001
04:33:23,408 DEBUG SessionImpl:1374 - updating [test.Stock#d1e22501fbd4255600fbd42559d30001]
04:33:23,410 DEBUG Cascades:497 - processing cascades for: test.Stock
04:33:23,412 DEBUG Cascades:506 - done processing cascades for: test.Stock
04:33:23,414 DEBUG Cascades:506 - done processing cascades for: test.OrderItem
04:33:23,416 DEBUG Cascades:497 - processing cascades for: test.OrderItem
04:33:23,417 DEBUG Cascades:506 - done processing cascades for: test.OrderItem
04:33:23,419 DEBUG Cascades:113 - cascading to saveOrUpdate()
04:33:23,421 DEBUG SessionImpl:1321 - saveOrUpdate() unsaved instance
04:33:23,429 DEBUG SessionImpl:786 - saving [test.OrderItem#d1e22501fbea3a9d00fbec39d1650010]
04:33:23,431 DEBUG Cascades:497 - processing cascades for: test.OrderItem
04:33:23,433 DEBUG Cascades:113 - cascading to saveOrUpdate()
04:33:23,435 DEBUG Cascades:341 - id unsaved-value strategy NULL
04:33:23,437 DEBUG SessionImpl:1326 - saveOrUpdate() previously saved instance with id: d1e22501fbd4255600fbd42559d30001
04:33:23,441 DEBUG SessionImpl:1374 - updating [test.Stock#d1e22501fbd4255600fbd42559d30001]

My Stock map is:

<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>

My Order map is:

<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>

My Order Item map is:

<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" cascade="save-update" column="stock_id"/>
</class>

As a point, if I have the following code:

InsertOrder insertorder = new InsertOrder();
newOrder = new Order();

Stock newStk = InsertOrder.checkStock("d1e22501fbd4255600fbd42559d30001");

newOrder.addProduct(newStk,1,32);
newOrder.addProduct(newStk,1,32);
newOrder.addProduct(newStk,1,32);
newOrder.addProduct(newStk,1,32);

insertorder.newOrder(newOrder);

it works fine. One order is created with 4 order items. what am I doing wrong??

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 1:28 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Quote:
The problem I am having comes when I uncomment point 5, to insert the new order to the table. So effectively, as this is a test page, what I am doing is refreshing the page a few times to create an order and then some order items. i then uncomment point 5 and refresh the page again, at this point the error is generated. The debug code I am getting is:

I'm not sure what you code for
Code:
insertorder.newOrder(newOrder);
looks like I suspect the following:

You are trying to insert and order that already exists (as hibernate has pointed out). i.e. You have created the order (using save I assume, then refreshed the page, and then using the same identifier for the order, are trying to save the order again.

Try using session.saveOrUpdate in your InsertOrder.newOrder() method. Alternatively:
Code:
//1.Determine if an order has been created
Order newOrder = (Order)session.getAttribute("ORDER");

if (newOrder == null){
newOrder = new Order();
}

//2.check that stock exists
Stock newStk = InsertOrder.checkStock("d1e22501fbd4255600fbd42559d30001");

//3.Add stock item
newOrder.addProduct(newStk,1,32);

//4.Update Session object
session.setAttribute("ORDER", newOrder);

//5.Save stock item
if (newOrder.getID() == null) {
    Insertorder.updateOrder(newOrder);
} else {
    Insertorder.newOrder(newOrder);
}


Where updateOrder =
Code:
session.Update(newOrder)



BTW is point 4 necessary? Surely point 3 is a reference to the same object in the session.


Top
 Profile  
 
 Post subject: Hi drj
PostPosted: Thu Apr 15, 2004 1:55 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
My code for InsertOrder neworder is:

public void newOrder (Object newStockItem)
{
Transaction transaction = null;
HibernateUtil Util = null;
Session session = null;

try {
Util = new HibernateUtil();
session = Util.currentSession();

transaction = session.beginTransaction();

session.saveOrUpdate(newStockItem);

transaction.commit();
Util.closeSession();

} catch (HibernateException sql) {
throw new RuntimeException("Exception in SQL:: " + sql.getMessage(), sql);

}
}

my class for Order is:

public Order() {
this.OrderDate = new Date();
}

/**
* Add a Product to this Order. Product automatically becomes an OrderItem.
* The priceTotal is automatically updated.
*
* @param p Product to add to this Order
* @param amount amount of products to add
*/
public void addProduct(Stock s, int amount, int user_ID) {
OrderItem orderItems = new OrderItem(this, s, amount);
this.PriceTotal = this.PriceTotal + s.getUnitCost() * amount;
this.UserID = user_ID;
this.OrderItems.add(orderItems);
}

public String getID() {
return ID;
}
public void setID(String string) {
this.ID = string;
}

public Set getOrderItems() {
return OrderItems;
}
public void setOrderItems(Set set) {
this.OrderItems = set;
}

public double getPriceTotal() {
return PriceTotal;
}
public void setPriceTotal(double d) {
this.PriceTotal = d;
}

public Date getOrderDate() {
return OrderDate;
}
public void setOrderDate(Date date) {
this.OrderDate = date;
}

public int getUserID() {
return UserID;
}
public void setUserID(int user_id) {
this.UserID = user_id;
}

my class for order item is:

public OrderItem() {
// empty default constructor
}

/**
* Creates valid OrderItem.
* @param order to which this OrderItem belongs
* @param p from which this OrderItem is created
* @param amount
*/
public OrderItem(Order order, Stock stock, int amount) {
Integer amountNew = new Integer(amount);
this.Order = order;
this.Stock = stock;
this.Amount = amount;
stock.setItemPurchaseAmount(stock.getItemPurchaseAmount() + amount);
this.Price = stock.getUnitCost() * amount;
}

private String ID;
private Stock Stock;
private Order Order;
private String StockID;
private String OrderID;
private double Price;
private int Amount;

public int getAmount() {
return Amount;
}
public void setAmount(int i) {
Amount = i;
}

public String getID() {
return ID;
}
public void setID(String string) {
ID = string;
}

public Order getOrder() {
return Order;
}
public void setOrder(Order order) {
this.Order = order;
}

public String getOrderID() {
return Order.getID();
}
public void setOrderID(String string) {
this.OrderID = string;
}

public double getPrice() {
return Price;
}
public void setPrice(double d) {
this.Price = d;
}

public Stock getStock() {
return Stock;
}
public void setStock(Stock stock) {
this.Stock = stock;
}

public String getStockID() {
return Stock.getID();
}
public void setStockID(String string) {
this.StockID = string;
}

my class for stock is:
public Stock(){
}

public StockImages getStockImages(){
return StockImages;
}
public void setStockImages(StockImages newStockImages){
this.StockImages = newStockImages;
}

public String getID() {
return ID;
}
public void setID(String newStockID) {
this.ID = newStockID;
}

public Integer getArtistID() {
return ArtistID;
}
public void setArtistID(Integer newArtistID) {
this.ArtistID = newArtistID;
}

public String getItemType() {
return ItemType;
}
public void setItemType(String newItemType) {
this.ItemType = newItemType;
}

public String getItemTitle() {
return ItemTitle;
}
public void setItemTitle(String newItemTitle) {
this.ItemTitle = newItemTitle;
}

public String getItemDemo() {
return ItemDemo;
}
public void setItemDemo(String newItemDemo) {
this.ItemDemo = newItemDemo;
}

public String getItemFinal() {
return ItemFinal;
}
public void setItemFinal(String newItemFinal) {
this.ItemFinal = newItemFinal;
}

public double getUnitCost() {
return UnitCost;
}
public void setUnitCost(double newUnitCost) {
this.UnitCost = newUnitCost;
}

public boolean getItemSelected() {
return ItemSelected;
}
public void setItemSelected(boolean newItemSelected) {
this.ItemSelected = newItemSelected;
}

public int getItemPurchaseAmount(){
return ItemPurchaseAmount;
}
public void setItemPurchaseAmount(int newItemPurchaseAmount){
this.ItemPurchaseAmount = newItemPurchaseAmount;
}

Peter


Top
 Profile  
 
 Post subject: On further investigation....
PostPosted: Thu Apr 15, 2004 2:12 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
I have manages to ascertain what i believe to be the problem. The problem lays at the fact that each subsequent creation of an order item is being created with a different session id, instead of utilising the session id created on the first order item creation:

[Order] id=null priceTotal=14.97 date=Thu Apr 15 07:04:11 BST 2004 Order items: [OrderItem] id=null amount=1 price=4.99 (test.Stock@cb150a) [OrderItem] id=null amount=1 price=4.99 (test.Stock@5be7f7) [OrderItem] id=null amount=1 price=4.99 (test.Stock@369cb9)

Of course when insertorder.neworder is called it falls over when the object attempted to be saved. Any ideas as to how I can over come this?

Peter


Top
 Profile  
 
 Post subject: Ok....
PostPosted: Thu Apr 15, 2004 7:56 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
forget the last post I sent, that didn't turn out to be true.... So I am still lost here. Anybody have any ideas as to what I am missing???

Peter


Top
 Profile  
 
 Post subject: NonUniqueObjectException:
PostPosted: Thu Apr 15, 2004 11:38 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
This exception is thrown when an operation would break session-scoped identity. This occurs if the user tries to associate two different instances of the same Java class with a particular identifier, in the scope of a single Session.

Ok, so what I want to know is why, and how do I resolve this issue in light of what i am trying to do? Is there a way i can kill the currently held class instance before I save the object held in the session to hibernate???

Peter


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