-->
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.  [ 1 post ] 
Author Message
 Post subject: collection persistence: what if I create a whole new list?
PostPosted: Tue Jun 26, 2007 5:07 am 
Newbie

Joined: Fri Apr 13, 2007 2:13 am
Posts: 2
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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.