Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the meaning of hibernate inverseJoinColumns?

Tags:

I understand hibernate @JoinTable annotation, but I don't understand the inverseJoinColumns. What is it used for?

e.g.

 @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
 @JoinTable(name = "stock_category", catalog = "mkyongdb", joinColumns = {
        @JoinColumn(name = "STOCK_ID", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "CATEGORY_ID",
                nullable = false, updatable = false) })

 public Set<Category> getCategories() {
    return this.categories;
}
like image 226
alecrosic Avatar asked Aug 07 '16 18:08

alecrosic


People also ask

What is spring boot inverseJoinColumns?

The element 'joinColumns' is used to specify foreign key column of the join table which references the source table (the owner of the association). The element 'inverseJoinColumns' is used to specify foreign key column of the join table which references the target table (the inverse side of the association).

How does @JoinTable work?

The @JoinTable annotation indicates that we will interact with the intermediary table (user_roles) and further you can see settings of the relationship including mapping of columns. The joinColumns attribute is responsible for the columns mapping of the owning side.

What is hibernate JoinTable?

The @JoinTable annotation is used to specify the table name via the name attribute, as well as the Foreign Key column that references the post table (e.g., joinColumns ) and the Foreign Key column in the post_tag link table that references the Tag entity via the inverseJoinColumns attribute.

What is referencedColumnName in @JoinColumn hibernate?

Quoting API on referencedColumnName: The name of the column referenced by this foreign key column. Default (only applies if single join column is being used): The same name as the primary key column of the referenced table.


2 Answers

From the javadocs, it means:

(Optional) The foreign key columns of the join table which reference the primary table of the entity that does not own the association

In layman's terms, it is the column of Category that will be used as a part of the JoinTable relationship between the current entity and Category.

If you don't specify joinColumns and inverseJoinColumns on the @JoinTable annotation, the persistence provider assumes a primary key to primary key join relationship and still store the equivalent ID columns for two related entities in the table by default.

like image 128
Naros Avatar answered Sep 19 '22 19:09

Naros


For example, we have 2 models: User and Role. The relationship between those 2 models will be Many-to-Many. User Model:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();

Role Model:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Enumerated(EnumType.STRING)
@Column(length = 20)
private ERole name;

The joinColumn attribute will connect to the owner side of the relationship, and the inverseJoinColumn to the other side

like image 24
Mubariz Mirzayev Avatar answered Sep 20 '22 19:09

Mubariz Mirzayev