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

前端 未结 1 1390
南旧
南旧 2021-01-21 07:05

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

相关标签:
1条回答
  • 2021-01-21 07:52

    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;
    
    ....
    
    0 讨论(0)
提交回复
热议问题