Doubt about foreign key Ibernate

Asked

Viewed 88 times

0

insert 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 Diary and place tables.inserir a descrição da imagem aqui

I want the result to be equal to 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;

1 answer

0

When you have two tables with many-to-many relationship you have to have a table in the middle. That’s why you have the diary_place. And to maintain the consistency of the data we use the two foreign keys as it is in the image. You can remove a key and use the other key to connect the two tables (d_user_id for example), but then it will give inconsistency in the data for sure.

  • I want it to be the same in the image I just added, only with a field referring to the user id. This way will not leave gap for inconsistency.

  • Well, that’s what I told you then. Both tables linking to the same foreign key in the middle table. If it conforms to the logic of your business, then you are correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.