JPA @EmbeddedId: How to update part of a composite primary key?

醉酒当歌 提交于 2019-12-01 23:59:39

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;

....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!