Search code examples
javagwtjparequestfactory

removing entities in OneToMany with RequestFactory & JPA


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.


Solution

  • I have answered your JPA question as well.

    Base on my past experience, removing a child in parent-child bi-directional relationship in Hibernate can be very tricky.

    What I usually do is use uni-directional mapping instead, i.e. Product does not hold the set of experts. But you can implement a getter to get all experts using Hibernate call. The advantages are:

    1. easier mapping
    2. easier coding
    3. manageable behviour

    You can always come back and implement caching or eager fetching at later stage. And most of the time they are unnecessary pre-mature optimization.