John Doe
John Doe

Reputation: 9764

Many-to-many relationship with composite keys in Hibernate

Is there a way to use @JoinTable annotation, connecting each table by two fields? Here are the example tables:

+----------------+  +----------------------------------+  +----------------+      
| example_table  |  | example_table_to_some_type_table |  | some_type_table|
+----------------+  +----------------------------------+  +----------------+   
|example_table_id|  |example_table_id                  |  |some_type_id    |
|another_id      |  |some_type_id                      |  |another_id      |
|...             |  |another_id                        |  |...             |
|other columns   |  +----------------------------------+  |other columns   |
|...             |                                        |...             | 
+----------------+                                        +----------------+

For a many-to-one case it would look like that:

@ManyToOne
@JoinColumns({ @JoinColumn(name = "some_type_id", 
    referencedColumnName = "some_type_id", 
    insertable = false, updatable = false),
               @JoinColumn(name = "another_id", 
    referencedColumnName = "another_id", 
    insertable = false, updatable = false) })
private SomeType someType;

But what about many-to-many relationship? I tried the following and it didn't work:

@ManyToMany(targetEntity = SomeType.class)
@JoinTable(name = "example_table_to_some_type_table", 
    joinColumns = { @JoinColumn(name = "example_table_id"),  
                    @JoinColumn(name = "another_id") }, 
    inverseJoinColumns = { 
                    @JoinColumn(name = "some_type_id"),  
                    @JoinColumn(name = "another_id") })
private Set<SomeType> someTypeSet;

I got the following error:

Repeated column in mapping for collection: com.package.name.Example.someTypeSet column: another_id

What should I look at and which part of this mapping is incorrect? Thanks in advance.

Upvotes: 1

Views: 1957

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

Your join table needs 4 columns. 2 to reference the PK columns of example_table and 2 other to reference the PK columns of some_type_table.

So it should look like

+----------------+  +----------------------------------+  +----------------+      
| example_table  |  | example_table_to_some_type_table |  | some_type_table|
+----------------+  +----------------------------------+  +----------------+   
|example_table_id|  |example_table_id                  |  |some_type_id    |
|another_id      |  |example_table_another_id          |  |another_id      |
|...             |  |some_type_id                      |  |...             |
|                |  |some_type_table_another_id        |  |                |
|other columns   |  +----------------------------------+  |other columns   |
|...             |                                        |...             | 
+----------------+                                        +----------------+

Upvotes: 2

Related Questions