cancel
Showing results for 
Search instead for 
Did you mean: 

CMP-EJB: Delete Item from Relation with Iterator.remove()?

Former Member
0 Kudos

Hi group,

I want to delete Items from an container managed relation. I make an iterator out of the obtained collection and use the remove() method. It doesn't work.

(I tried as well the Collection.remove(object) - it doesn't work either..)

Is there any trick I have overseen?

Thanks for any kind of ideas! Regards,

Simon

P.S: Some of my coding.. (Usage is in a JavaBean Model for WebDynpro)

The "remove" part is reached correctly - but there is no effect...


						java.util.Collection itemEJBs = plh.getPackingListItems();
System.out.println( Integer.toString(itemEJBs.size()) + "Items");
						Iterator it_beans; 
						
							Iterator it_ejbs = itemEJBs.iterator();
							while (it_ejbs.hasNext()){
								
								PackingListItemEJBLocal pli = (PackingListItemEJBLocal) it_ejbs.next();
								
								// Position still in beans?
									it_beans =  plis.iterator();
									boolean found = false; PackingListItemBean it_bean = null;
									
									while (it_beans.hasNext() && ! found)
									{ 
										it_bean = (PackingListItemBean) it_beans.next();
										if (pli.getPosNr().equals(it_bean.getPosNr())){
											found = true;
										}
									}

									if (! found){
										//remove EJB (its not in beans anymore)
											it_ejbs.remove();
											System.out.println("Loop - EJB deleted (Relations)! : " + pli.getPosNr());
										}else{							
										// do nothing
									}
								
							}
						System.out.println("Items in Collection: " + Integer.toString(itemEJBs.size()) + "Items.");
							

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

hello simon,

iterator.remove() method is an optional operation

as defined in the Java API. thus this depends on

the underlying Collection object. check on the javadoc

of the underlying Collection object i.e. the implementation

class if it supports or not.

the following line returns a Collection interface.

any idea of the implementing class?

java.util.Collection itemEJBs = plh.getPackingListItems();

regards

jo

Former Member
0 Kudos

Hi Jo,

thanks for your reply -

this could be an explenation - as well for the fact, that the remove(Object o) method of the underlying Collection doesn't work either! (So i think its not implemented)

I dont know what type it is - it comes from an cmr-relation with radiobutton "Collection" market (in NWDS). I didn't find yet, how the SAP-J2EE implements this.

But how do you the remove elements from CMP-Relations? Make a new collection and replace the old one?

Thanks a lot,

Simon

-


edit: The implementing class seems to be com.sap.engine.services.ejb.entity.pm.multiple.ReferenceCollection

Method remove returns false...

Message was edited by: Simon Prinzleve

Former Member
0 Kudos

hello simon,

are you getting a collection instance of the cmp relations

that are declared in the ejb-jar.xml?

replacing an old one with the new collection is the safest

way it seems.

regards

jo

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Simon,

I think the problem is with the Iterator. The methods in Iterator "hasNext() and next()" is dynamically changing when u remove or add items from the Object for which you have created the Iterator.

So try using some other Collection object like ArrayList. First use Iterator and add all the Objects in the Iterator to the ArrayList. Then do your logic using the ArrayList. This will work properly.

If you can get the items as ArrayList collection then you can directly use your logic on this ArrayList.

Hope this helps.

regards

Venkat

Former Member
0 Kudos

Thanks for your replies!

I did it as two of you recommended: I constructed a new collection and replaced the complete CMP-relation.

(Maybe the ArrayList Approach would habe been possible as well - I didn't try out that one..)

Thanks!

Simon

Former Member
0 Kudos

Hi Simon,

If I got you correctly, you want to delete an entity object. You should invoke remove() method on the entity object itself. If you invoke remove() method on the Collection that will remove the instance only from the Collection itself, not from the database.

If you find entity object PackingListItemEJBLocal pil = ejb_it.next(), that is the entity you want to delete then just invoke its pil.remove() method. The EJB container will automatically remove this instance from all relationships in which it takes part. It is unnecessarily to create a new Collection of entities and substitute it in the relationships.

Regards, Viktoria

Former Member
0 Kudos

Hi Viktoriya,

thanks for your additional information!

Main main purpose was to remove it only from the relationship - but you are rigth: In this special case (and maybe in most) the deletion from the DB (which I postponed to later is desired as well

So thanks for your hint!

Thanks and regards,

Simon

Former Member
0 Kudos

Hi Simon,

Since both the remove() methods in Iterator and Collection are optional operations, you can try defining a new Collection object. Add the PackingListItemEJBLocal object to the new collection if the "found" variable is true.

Regards,

Uma