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.  [ 2 posts ] 
Author Message
 Post subject: Cannot delete database rows: using Iterator.remove()
PostPosted: Tue Nov 27, 2007 7:36 pm 
Newbie

Joined: Tue Nov 27, 2007 6:57 pm
Posts: 1
I have an object ObjectA which has a property Set, having collection of objects ObjectB,

i.e in ObjectA class, i have
private Set objectB = new HashSet();

I want to delete one of the collection objects, "ObjectB", associated with ObjectA
(i.e one of the objects in Set objectB as mentioned above)


--- the mapping from ObjectB to ObjectA is MANY-TO-ONE
--- the associating COLUMN in ObjectB is ObjectAId, as below

--- this is the code of ObjectB Class
/**
* @return com.abc.xyz.ObjectA
*
* @hibernate.many-to-one
* column="ObjectAId"
* not-null="true"
* lazy="false"
* @hibernate.cache usage="read-write"
*/
public ObjectA getObjectA() {
return objectA;
}

Please note that the mapping column attribute,
not-null="true"

what i do is ::

for ( Iterator itr = ObjectA.getObjectB().iterator(); i.hasNext();) {
ObjectBClass objB = (ObjectBClass) itr.next;
if (objB.getName().equals("ABC")) {
itr.remove();
}
}

When i save ObjectA... the following is the error ::

ERROR:
java.sql.SQLException: Cannot insert the value NULL into column 'ObjectAId', table 'Table-OBJECTB'; column does not allow nulls. UPDATE fails.

I understand that when i make the not-null attribute to "false"
it works, i.e. if i do :: not-null="false" it deletes the row in the table.

BUT is there any other way I can delete, without changing the Mapping attribute ??

When I change the not-null="false"
-- it sets the column value of the ObjectB table, to NULL


Please let me know if I can delete, without changing the mapping.

Thanks,
anand


Top
 Profile  
 
 Post subject: Cannot delete database rows: using Iterator.remove()
PostPosted: Fri Nov 30, 2007 4:10 am 
Newbie

Joined: Thu Apr 13, 2006 3:39 am
Posts: 4
Location: India
There is no need to save the object when you remove any element from that object's collection.
what you have done is removed the element using iterator.remove(). Thats it. You just need to commit your transaction and the corresponding elelemnt will get deleted from the database.
I have implemented this. It works fine at my end.

Vishal


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