views:

160

answers:

3

Hi

I have a problem with JPA (EclipseLink). I am not able to delete a association table. This is the situation:

  • Product 1:n to ProductResource
  • Resource 1:n to ProductResource

I first set the product and resource attributes of ProductResource. If I then try to delete the ProductResource object nothing happens (no sql is generated - no exception). If I comment out both OneToMany annotations in ProductResource I can delete the object. I can also delete the object when product and resource attributes are not set. If I comment out only the annotation above the ressource attribut the ProductResource object gets deleted upon the deletion of the product object (cascade=CascadeType.ALL). I hope someone could give me a hint. Thank you.

Product Resource:

public class ProductResource implements Serializable {
 @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
 private Product product;

 @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
 private Resource resource;

Product:

public class Product implements Serializable {

 @OneToMany(mappedBy="product", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
 private List<ProductResource> productResources = new ArrayList<ProductResource>();

Resource:

public class Resource implements Serializable {

 @OneToMany(mappedBy="resource", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
 private List<ProductResource> productResources = new ArrayList<ProductResource>();

Greetings Marcel

A: 

The following was the solution:

Product class

@PrivateOwned
@OneToMany(mappedBy="product", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<ProductResource> productResources = new ArrayList<ProductResource>();

Resource class

@PrivateOwned
@OneToMany(mappedBy="resource", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<ProductResource> productResources = new ArrayList<ProductResource>();

@PrivateOwned is new...

Marcel

Marcel Menz
A: 

There are actually 3 solutions:

1) Delete the orphans before you delete the ProductResource object. The reason that the ProductResource was not being deleted, is because there were still objects left in the system that reference them.

2) Remove the references in the orphans to the ProductResource object. This is for the same reason as above.

3) Set the Product and Resource objects as @PrivateOwned using the JPA Annotations. This will cause the orphans to automatically be deleted if they exist. This is behavior that you may or may not want to be done automatically for you. A reason for this may be because a Product or Resource object does not need a reference to ProductResource to exist. It depends on your design.

Kevin Crowell
Thank you very much for your detailed explanation. At first JPA looks very easy to use. The devil is in the detail. Great that people like you spend their time helping out a novice like me.Greetings Marcel
Marcel Menz
A: 

Hi I have similar problems

even i used privateowned my child orphans are not getting deleted below is my code for updating records, I have group entity which is having list of portfolios my update method goes like this.

    entr.begin();
    Group group = em.find(Group.class,
            (long) 18200);

    Group groupToBeUpdated1 = em
            .merge(Group);
    for (GroupsDetail groupsDetail : ibPortfolioGroup
            .getGroupsDetails()) {

        groupsDetail.setIbPortfolioGroup(null);

    }



    GroupsDetail groupsDetail = new IGroupsDetail();
    groupsDetail.setPortfolioNo("1777707-21");
    groupsDetail.setSysDate(new Date());
    groupsDetail.setIbPortfolioGroup(ibPortfolioGroupToBeUpdated1);

    GDetail groupsDetail1 = new GroupsDetail();
    groupsDetail1.setPortfolioNo("1777707-22");
    groupsDetail1.setSysDate(new Date());
    groupsDetail1.setIbPortfolioGroup(ibPortfolioGroupToBeUpdated1);

    ibPortfolioGroupToBeUpdated1.getIbPortfolioGroupsDetails().add(
            groupsDetail);
    ibPortfolioGroupToBeUpdated1.getIbPortfolioGroupsDetails().add(
            groupsDetail1);
    em.flush();

can u guide me where am i doing wrong??? I specified @privateowned as

@PrivateOwned @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "ibPortfolioGroup") @OrderBy("portfolioNo") private List groupsDetails;

softdeveloper