Reputation: 462
I'm currently using EclipseLink 2.5 for a project and encountered a little problem with a ManyToMany Relation:
I have a mapped superclass Identifiable
that defines a composite key (uuid, revision). Both Entity classes Appointment
and Content
are subclasses of Identifiable
.
Now I try to define an unidirectional ManyToMany relation from Appointment
to Content
, but it seems that EclipseLink doesn't create the JoinTable right. There are only two columns in it (uuid, revision), but there should be four (one uuid and revision for each side of the relation).
Lust but not least, the code:
@MappedSuperclass
@IdClass(IdentifiablePK.class)
public abstract class Identifiable implements Serializable {
@Id
@UuidGenerator(name = "uuid")
@GeneratedValue(generator = "uuid")
protected String uuid;
@Id
protected Integer revision;
/* ... */
}
public class Appointment extends Identifiable {
@JoinColumns({
@JoinColumn(columnDefinition = "trainingcontent_uuid", referencedColumnName = "uuid", nullable = false, updatable = false, insertable = false),
@JoinColumn(columnDefinition = "trainingcontent_revision", referencedColumnName = "revision", nullable = false, updatable = false, insertable = false)
})
@ManyToMany(fetch = FetchType.LAZY)
protected List<TrainingContent> trainingContents;
/* ... */
}
public class TrainingContent extends Identifiable {
/* ... */
}
I also tried to use @JoinTable
instead of @JoinColumns
, but then EclipseLink complains about the missing resepectively incomplete @JoinColumns
Annotation, which is necessary for target entities with composite keys.
Am I doing something wrong?
Greetings, chrert
Upvotes: 1
Views: 920
Reputation: 18379
For ManyToMany you must use a @JoinTable to @JoinColumns, the source and target join columns are specified inside the @JoinTable.
Your @JoinColumn must not have, "updatable = false, insertable = false", this makes no sense, as then it would not be able to insert anything, which appears to be what is happening.
See, https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany
Upvotes: 1