Jason Zhicheng Li wrote:
Quote:
order.getItems().remove(new Item(1));
You are mapping Item as a composite-element in the set. That is, no identifier for each row. Hibernate figures out which row to delete by checking all properties to see if they are matched.
When you are issuing remove(new Item(1)), I am assuming you only set the value 1 to one property, where is the second one?
All the properties of Item should be not-null. Also you should implement equals() and hashCode() in Item.java.
new Item(1) is a constructor which requires a primary key as an argument. Both Item class and Order class have already implemented equals() and hashCode().
However if I perform the following:
order.setItems(new TreeSet());
tx.commit();
All item records associated with this order record are removed.
So In order to remove a record, do I need to do the following??
TreeSet set = new TreeSet();
Iterator it = order.getItems().iterator();
while (it.hasNext())
{
Item item = (Item) it.next();
if (item is not equal to the intend removed record)
set.add(item);
}
order.setItems(set);
tx.commit();
Do I really need to do all these just to remove a single record??