Mapping class for model Many-To-Many

I have two tables linked many to many through an adjacent table. There are only two attributes in this adjacent table, and these two attributes are a composite key. The foreign keys to these attributes in the linked tables are not related to the primary keys of these tables. In this case, how do I map the tables?

image

Hi

You need to specify referencedColumnName attribute for @JoinColumn annotations.

    @JoinTable(name = "ADJACENT_TBL",
            joinColumns = @JoinColumn(name = "ANOTHER_CODE", referencedColumnName = "CODE"),
            inverseJoinColumns = @JoinColumn(name = "CODE", referencedColumnName = "ANOTHER_CODE"))
    @ManyToMany
    private List<Table2> tables2;
1 Like