سؤال

I have defined a unidirectional ManyToMany relationship like this:

@Entity
@Table(name = "ODE_PROCESS_INSTANCE")
Public class ProcessInstanceDAOImpl extends OpenJPADAO implements ProcessInstanceDAO {
  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long _instanceId;

  @ManyToMany(targetEntity = PolicyAttachmentDAOImpl.class)
  private Collection<PolicyAttachmentDAO> _policyAttachments = new ArrayList<PolicyAttachmentDAO>();

  public ProcessInstanceDAOImpl() {
  }

  public Collection<PolicyAttachmentDAO> getPolicyAttachments(){
    return _policyAttachments;
  }

  public void setPolicyAttachments(Collection<PolicyAttachmentDAO> policyAttachments){
    _policyAttachments = policyAttachments;
    getEM().merge(this);
    getEM().flush();
  }
}

@Entity
@Table(name = "ODE_POLICY_ATTACHMENT")
public class PolicyAttachmentDAOImpl extends OpenJPADAO implements PolicyAttachmentDAO{
  @Id
  @Column(name = "POLICYATTACHMENT_ID")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long _attachmentId;

  @Basic
  @Column(name = "POLICYATTACHMENT_NAME")
  private String _attachmentName;

  public PolicyAttachmentDAOImpl(String policyAttachmentName) {
    _attachmentName = policyAttachmentName;
  }

  public void deletePolicyAttachment(){
    //delete policy attachment DAO
getEM().remove(this);
getEM().flush();
  }
}

The join table is created. The problem is that when I delete a record from ODE_POLICY_ATTACHMENT the join table is not updated. How can I solve this problem?

This is how I add to the ta

Collection<PolicyAttachmentDAO> policyAttachments = new ArrayList<PolicyAttachmentDAO>();

attachmentDAO = new PolicyAttachmentDAOImpl("attachment_1");
policyAttachments.add(attachmentDAO);

attachmentDAO = new PolicyAttachmentDAOImpl("attachment_2");
policyAttachments.add(attachmentDAO);

attachmentDAO = new PolicyAttachmentDAOImpl("attachment_3");
policyAttachments.add(attachmentDAO);

//inst is an exisiting ProcessInstanceDAOImpl
inst.setPolicyAttachments(policyAttachments);

To delete a record from "ODE_PROCESS_INSTANCE" table I call the method deletePolicyAttachment() on a PolicyAttachmentDAOImpl object.

هل كانت مفيدة؟

المحلول

The join table is what maps the many to many association. It's what allows Hibernate to know that some attachment is linked to some process. So, if you don't want an attachment to be referenced in the join table, that means that you don't want the attachment linked to any process anymore. So the answer is simple: find all the processes having the attachment in their collection, and remove the attachment from their collection:

process.getPolicyAttachments().remove(theAttachment);

Side note: your naming is awful. Prepending an underscore to fields is not a standard convention, and will force you to add this leading underscore in all your queries. And what you have there are not DAOs, but entities. DAOs are the objects used to query and update the databases. Entities are the persistent objects returned by the DAOs. And you should not merge and flush each time you change the attachments of a process. Merging is rarely necessary, and flushing almost never, and this shouldn't be done by an entity anyway, which shouldn't access the EM at all.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top