-->
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.  [ 9 posts ] 
Author Message
 Post subject: unable to delete an element in a collection...
PostPosted: Fri Sep 16, 2005 3:56 pm 
Newbie

Joined: Fri Sep 09, 2005 11:11 am
Posts: 19
<class name="Order" table="ORDERS">
<id name="id" column="ID"/>
<set name="items" table="ITEMS" cascade="all">
<key column="ID"/>
<composite-element class="Item">
<property name="info1"/>
<property name="info2"/>
</set>
</class>

==============

public class Order
{
private String id;
private Set items;
/* getter and setter methods */
}

public class Item
{
private String info1;
private String info2;
/* getter and setter methods */
}


public class Main
{
public static void main (String [] args)
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
try
{
Order order = (Order) session.get(Order.class, "12");
order.getItems().remove(new Item(1));
/* just removed one element from the collection! */
tx.commit();

}
catch (Exception e)
{}
finally
{
HibernateUtil.closeSession();
}
}
}
===============

I can see the SQL delete statement executes successfully in the debug log, but when I goto the database, the corresponding record is still exist..... Any idea??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 4:18 pm 
Newbie

Joined: Fri Apr 22, 2005 3:21 pm
Posts: 12
instead of cascade = all

try cascade="all-delete-orphan"

also you might have to drop the relationship first before droping the parent.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 4:29 pm 
Newbie

Joined: Fri Sep 09, 2005 11:11 am
Posts: 19
stazione_jaxrpc wrote:
instead of cascade = all

try cascade="all-delete-orphan"

also you might have to drop the relationship first before droping the parent.


I have changed cascade="all-delete-orphan", but I am still unable to delete an element in the collection. I am not goint to delete any Order objects. I just want to delete some Items object in the collection.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 5:47 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
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.

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 6:37 pm 
Newbie

Joined: Fri Sep 09, 2005 11:11 am
Posts: 19
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??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 9:58 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
You are still confused by entity type versus value type. In your Order mapping file:
Quote:
<set name="items" table="ITEMS" cascade="all">
<key column="ID"/>
<composite-element class="Item">
<property name="info1"/>
<property name="info2"/>
</composite-element>
</set>


you are using value type for Item, that is, there is no such thing as primary key for an item. It seems to me what you are trying to do is a straightfoward unidirectional one-to-many association. Please read reference document on one-to-many association. If you still have problem, you can post back. Good luck!
[/code]

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 9:35 am 
Newbie

Joined: Fri Sep 09, 2005 11:11 am
Posts: 19
Jason Zhicheng Li wrote:
You are still confused by entity type versus value type. In your Order mapping file:
Quote:
<set name="items" table="ITEMS" cascade="all">
<key column="ID"/>
<composite-element class="Item">
<property name="info1"/>
<property name="info2"/>
</composite-element>
</set>


you are using value type for Item, that is, there is no such thing as primary key for an item. It seems to me what you are trying to do is a straightfoward unidirectional one-to-many association. Please read reference document on one-to-many association. If you still have problem, you can post back. Good luck!
[/code]


You are correct that Item is a value type. I should have said that property 'info1' is used to distinguish between Item objects. The constructor of Item class requires an argument for 'info1' property.


Top
 Profile  
 
 Post subject: composite-element remove from collection
PostPosted: Tue Oct 18, 2005 6:04 am 
Newbie

Joined: Tue Oct 18, 2005 5:57 am
Posts: 1
We are facing the same but without success.
The question is: how to remove an element from a collection? and from database?


Top
 Profile  
 
 Post subject: unable to delete an element in a collection...
PostPosted: Tue Oct 18, 2005 2:46 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Here is what you need to do to delete an item from collection

Item wanted = null;
Collection items = order.getItems();
Iterator iter = items.iterator();
while (iter.hasNext()){
Item item = (Item) iter.next();
if (item.id matches the one you want)
wanted = item;
}

items.remove(wanted);
order.setItems(items);

// close session



-------
Hope it Helps.

Ron
---
Dont forget to rate !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.