I have a many-to-many relationship where the link table has an additional property. Hence the link table is represented by an entity class too and called Composition
Why not just change the primary key of Composition
to be a UID which is auto-generated? Then the users could change the two references to the entities being joined without having to delete/re-create the Composition
entity. Optimistic locking would then be maintained.
EDIT: For example:
@Entity
@Table(name = "COMPOSITION")
public class Composition {
@Id
@Column(name = "ID")
private Long id; // Auto-generate using preferred method
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn( .... as appropriate .... )
private FirstEntity firstEntity;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn( .... as appropriate .... )
private SecondEntity secondEntity;
....