Many-to-many on the same table with additional columns

后端 未结 3 516
终归单人心
终归单人心 2020-12-08 23:24

I have a class User. A user can be a friend with many other users. The relationship is mutual. If A is a friend of B then B is a friend of A. Also I want every relation to s

3条回答
  •  独厮守ぢ
    2020-12-09 00:03

    I'm not sure this will fit your case, but give it a try.

    @Entity
    public class Friend {
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int friendId;
    
        @Column
        private String name;
    
        @ManyToMany(mappedBy="owner")
        private List friendships;
    }
    
    @Entity
    public class Friendship {
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int friendshipId;
    
        @OneToOne
        private Friend owner;
    
        @OneToOne
        private Friend target;
    
       // other info
    }
    

提交回复
热议问题