Quantcast
Channel: Apache Timeline
Viewing all articles
Browse latest Browse all 5648

ManyToMany tries to insert twice

$
0
0
Hi, im not sure if i have a clear concept about the Many to Many persist.

I have this scenario.

table: courses
-course_id
-description

table: documents
-docuemnt_id
-description

table: courses_documents
-course_id
-docuemnt_id

Then my entities:

public class Course{

private int courseId;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "courses_documents",
inverseJoinColumns =
@JoinColumn(name="course_id"),
joinColumns=
@JoinColumn(name="document_id"))
private Set<Document> documents;

public class Document{

private int documentId;

@ManyToMany(mappedBy="documents"
private Set<Course> courses;

Then when i want to persist i did this.

Course course = em.merge(aCourse);

//i get a reference of an existed document
Document doc1 = entityManager.getReference(...);

//then i add that document to course
course.getDocuments().add(doc1);

then jpa tries to insert twice in courses_documents why twice??

Thanks

Viewing all articles
Browse latest Browse all 5648

Trending Articles