paul
paul

Reputation: 13471

Hibernate constraint with three entities

Let´s say that I have entities A, B and C. I would like to create a constraint between this three tables. How can I accomplish that?. I ´ve been looking and what people normally do is create a table with just the id of A and B and then configure on his classes a ManyToMany relationship like this one.

      @ManyToMany(cascade = {CascadeType.MERGE})
@JoinTable(name = "A_B",
        joinColumns = @JoinColumn(name = "A_id"),
        inverseJoinColumns = @JoinColumn(name = "B_id"),
        uniqueConstraints = @UniqueConstraint(columnNames = {"A_id", "B_id"}))

But how can I do it for three classes?.

Regards.

Upvotes: 0

Views: 192

Answers (1)

OO7
OO7

Reputation: 2807

@MapKeyJoinColumn : Specifies a mapping to an entity that is a map key. The map key join column is in the collection table, join table, or table of the target entity that is used to represent the map. If no MapKeyJoinColumn annotation is specified, a single join column is assumed and the default values apply.

Have a look at this :- @MapKeyJoinColumn in API doc & Hibernate: How to Join three 3 tables in one join table in Annotation.

Upvotes: 1

Related Questions