Back Forum Reply New

DataIntegrityViolationException on save

Hi,
I am using hibernate 3 with spring and mysql 5. I have 2 classes..InventoryItem and InventoryLocation. InventoryItem records have reference to an InventoryLocation, and many items can have the same location reference (ie a location can hold many items).

When I create a new item which references a location that is already referenced by another existing item, I get a DataIntegrityViolationException. But the item gets saved and the error is thrown after the record is inserted into the db. So it seems spring is trying to do a commit after the save method is called and thats where it encounters a problem.

From the stack trace it seems an sql method is causing the problem :
update inventory_item set inventory_item
_id=? where inventory_item_id=?]; Duplicate entry '15' for key 1
(the number 15 is the id of the location)

The hibernate mapping files and resulting exception trace are attached. The receive_item  method in the manager after which the exception is thrown is shown below...We have been using these technologies together for a few projects and this is the first time we are seeing this problem. Hope someone can help..

This is what the table looks like..as you can see...product_id and location_id have MUL in the Key column..
mysqlgt; desc inventory_item;
+-------------------+------------+------+-----+---------+----------------+
| Field             | Type       | Null | Key | Default | Extra          |
+-------------------+------------+------+-----+---------+----------------+
| inventory_item_id | bigint(20) | NO   | PRI | NULL    | auto_increment |
| product_id        | bigint(20) | YES  | MUL | NULL    |                |
| quantity          | bigint(20) | YES  |     | NULL    |                |
| date_created      | datetime   | YES  |     | NULL    |                |
| location_id       | bigint(20) | YES  | MUL | NULL    |                |
+-------------------+------------+------+-----+---------+----------------+
5 rows in set (0.05 sec)Thanks,
-Riz.

public void receiveItem(InventoryItem item, long userId, String comment) {

InventoryLocation location = inventoryDAO.getLocation(item.getLocationId());
message = quot;Added quot; + item.getQuantity() + quot; units to location quot; + location.getLocationCode();;
item.setLocation(location);
inventoryDAO.saveItem(item);
location.getItems().add(item);
inventoryDAO.saveLocation(location);
}
¥
Back Forum Reply New