tags:

views:

66

answers:

1
Report.entity {
  @ManyToMany
  @JoinTable(name = "reports_contents_relations",
             joinColumns = @JoinColumn(name = "report_id"),
             inverseJoinColumns = @JoinColumn(name = "content_id"))
  @IndexColumn(name="content_order")
private List contents = new ArrayList();
}

Someclass {
  public void remoteContentFromReport(Content content) {
    List contents = report.getContents();
    contents.remove(content);
    save(report);
  }
}

When calling remoteContentFromReport method I get the following error.

java.sql.BatchUpdateException: Duplicate entry deleting from collection

I don't want to delete the Content.entity, just the entry in the join table associating it to a report.

What am I missing?

A: 

You should be able to just call report.saveOrUpdate() and it will automatically unlink all content that is not present in report.contents list (if everything is mapped correctly).

So get your report, remove unneeded content entries from the contents list, and save it.

serg
I'm using spring's entity manager with jpa, so I only have merge or persist. But calling merge on the report entity, after removing my item from the collect, throws the Duplicate entry error.
dom farr
I've written a very simple test app using the simplest environment and it works as I would have expected. I therefore think this isn't a problem with JPA/Hibernate, or the mapping, but more likely in the abstraction layer I've written in between my controllers and spring; probably something to do with transactions.
dom farr