Announcement Announcement Module
No announcement yet.
The best way to update JPA managed entity relations Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • The best way to update JPA managed entity relations


    I have data model like this:

    public class Customer {
       private Collection<Sale> purchases;
       // other properties, setters, getters
    public class Sale {
       private Collection<SaleItem> items;
       // other properties, setters, getters
    public class SaleItem {
       // properties, setters, getters
    JPA is used at persistence layer. I have a dialog and form to edit Customer details.
    My question is - how efficiently merge changes from form to external storage?
    In a ideal case I think it would look like this:
    1. I pass a Customer instance to dialog;
    2. After user clicks Ok button form commits changes to original Customer object;
    3. Customer DAO object's merge(customer) method is called in dialog's onFinish method to persist all changes.

    I see a problem in a step 3. It works if new instances of Sale and SaleItem are created and added to corresponding collections. But if some instances are removed - JPA doesn't remove them from storage. JPA hasn't features like TopLink Private Ownership or Hibernate delete-orphan cascade type.
    At the moment I have just one idea how to solve the problem. When a dialog window opens make a deep copy of my Customer instance and when the window closes iterate through all collections to detect instances that were removed. With a list of removed Sale and SaleItem instances I can call DAO remove method for each of them.

    Maybe someone has a better solution?


  • #2
    We use JPA specification annotations and just only delete-orphan hibernate propietary annotation for such cases.

    It seems JPA will never support this feature.