0

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.

paul
  • 12,873
  • 23
  • 91
  • 153
  • Yes you can do this. This link may helps you to clear your idea : http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/ – Keval Trivedi Jul 10 '14 at 12:15
  • This link do what I paste, a relationship many to many between two entities. My question was how to do it with 3 – paul Jul 10 '14 at 12:18

1 Answers1

1

@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.

Community
  • 1
  • 1
OO7
  • 2,785
  • 1
  • 21
  • 33