Removing entities in OneToMany with RequestFactory & JPA

479 views
Skip to first unread message

Roalt

unread,
Oct 29, 2011, 2:49:22 PM10/29/11
to google-we...@googlegroups.com

I would like to know what's the right way to remove a child from a one-to-many relation within GWT using the RequestFactory.

My GWT application with an Entity called Product and that product has a one-to-many relation to an Expert:

@Entity
public class Product {
   
...
   
OneToMany(mappedBy="product", orphanRemoval=true,
              cascade
={CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},fetch=FetchType.EAGER)
   
Set<Expert> experts = new HashSet<Expert>();
   
...
}

@Entity(name = "EXPERT")
public class Expert {
   
...
   
@ManyToOne(optional=false)
   
Product product;
   
...
}

I have a user-interface where you can change some values of Product, but also a window where experts can be added or removed. Adding a Expert goes well, but how do I remove an expert? And what administration must I do on the client and server side?

I have already an opened productRequest going on.

I would appreciate all the help!


Patrick Julien

unread,
Oct 29, 2011, 5:53:00 PM10/29/11
to google-we...@googlegroups.com
add CascadeType.Delete or use ALL, then removing the object from the collection will be sufficient to remove it from the underlying storage

product.getExperts().remove(expert);

Roalt

unread,
Oct 30, 2011, 4:24:40 AM10/30/11
to Google Web Toolkit
When I add the Cascade.REMOVE, I get the following error:

Caused by: javax.persistence.EntityNotFoundException: deleted entity
passed to persist: [myapplication.server.domain.Expert#<null>]

this happens after I called my save() function in
server.domain.Product.java:

em.getTransaction().begin();
em.merge(this);
em.getTransaction().commit();

...so that was the reason for me to remove the Cascade.REMOVE.

Patrick Julien

unread,
Oct 30, 2011, 7:02:42 PM10/30/11
to google-we...@googlegroups.com
do you have nulls in your expert collection in your product object?

If you have detach in your CascadeType, it works anyway if you do this

product.getExperts().remove(expert);
em.merge(product);
em.remove(expert);

The remove cascade type removes the need for a distinct remove call.

Patrick Julien

unread,
Oct 30, 2011, 7:27:57 PM10/30/11
to google-we...@googlegroups.com
In fact, you don't even need to call merge either since you're inside a transaction
Reply all
Reply to author
Forward
0 new messages