Let's say I have loaded a Category with a list of 4 Items:
Code:
category.getItems().size(); // == 4
Now, I want my category to have only 1 element, a new one.
If I do the following:
Code:
category.setItems(new ArrayList<Item>());
category.getItems().add(new Item());
the "old" items are not erased from DB.
Next load will bring back 5 items!
But if I do the following instead:
Code:
category.getItems().clear();
category.getItems().add(new Item());
then the "old" items are deleted.
Isn't it a strange behaviour?
How can I control that the list of items is persisted as it is in memory for a category?
Here is the mapping:
Code:
<class name="Category" table="CATEGORY">
<id name="id" column="CATEGORY_ID" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">SQ_CATEGORY</param>
</generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<bag name="items" lazy="false" cascade="all-delete-orphan">
<key column="CATEGORY_ID" not-null="false" />
<one-to-many class="Item" />
</bag>
</class>
<class name="Item" table="ITEM">
<id name="id" column="ITEM_ID" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">SQ_ITEM</param>
</generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<many-to-one name="category" column="CATEGORY_ID" unique="true" not-null="true" class="Category" />
</class>