Doubt about foreign key hibernate

0

insert the image description here

The diary_place table is being created with 2 user_id field, I want it to be only a user_id referenced as foreign key for the diary and place tables.

I want the result to be the same as the second image.

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name ="diary_place",
           joinColumns = { 
                          @JoinColumn(name = "diary_id", referencedColumnName="id"), 
                          @JoinColumn(name = "d_user_id", referencedColumnName="user_id" ) 
                         },                    
           inverseJoinColumns = { 
                                 @JoinColumn(name = "place_id", referencedColumnName="id", nullable=false), 
                                 @JoinColumn(name = "p_user_id", referencedColumnName="user_id") 
                                },
           foreignKey=@ForeignKey(name="fk_diary"),
           inverseForeignKey=@ForeignKey(name="fk_place")
          )
private List <Place> places;
    
asked by anonymous 07.11.2018 / 19:35

1 answer

0

When you have two tables with many-to-many relationships you have to have a table in between. That's why it has diary_place. And to maintain the consistency of the data uses the two foreign keys as it is in the image. You can remove a key and use the other key to bind the two tables (d_user_id by ex), but it will give data inconsistency for sure.

    
07.11.2018 / 19:47