I've created two JPA entities (Client, InstrumentTraded) using Hibernate as a provider that have a ManyToMany relationship. After letting Hibernate generate the tables for MySQL it appears that the ManyToMany relationship table does not contain primary keys for the two foreign keys. This allows duplicate records in the many-to-many table, which is not the desired result.
Tables generated:
client(id,name)
instrument_traded(id,name)
client_instrument_traded(FK client_id, FK instrument_traded_id)
Preferred table:
client_instrument_traded(PK,FK client_id, PK,FK instrument_traded_id)
Entities:
@Entity
public class Client extends AbstractEntity<Integer> {
private static final long serialVersionUID = 1L;
@Basic(optional = false)
@Column(nullable = false, length = 125)
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(joinColumns = {
@JoinColumn}, inverseJoinColumns = {
@JoinColumn(name = "instrument_traded_id")}, uniqueConstraints =
@UniqueConstraint(name = "UK_client_instruments_traded_client_id_instrument_traded_id",
columnNames = {"client_id", "instrument_traded_id"}))
@ForeignKey(name = "FK_client_instruments_traded_client_id",
inverseName = "FK_client_instruments_traded_instrument_traded_id")
private List<InstrumentTraded> instrumentsTraded;
public Client() {
}
public List<InstrumentTraded> getInstrumentsTraded() {
return instrumentsTraded;
}
public void setInstrumentsTraded(List<InstrumentTraded> instrumentsTraded) {
this.instrumentsTraded = instrumentsTraded;
}
...
}
@Entity
@Table(uniqueConstraints = {
@UniqueConstraint(name = "UK_instrument_traded_name", columnNames = {"name"})})
public class InstrumentTraded extends AbstractEntity<Integer> {
private static final long serialVersionUID = 1L;
@Basic(optional = false)
@Column(nullable = false, length = 50)
private String name;
@ManyToMany(mappedBy = "instrumentsTraded", fetch = FetchType.LAZY)
private List<Client> clients;
public InstrumentTraded() {
}
public List<Client> getClients() {
return clients;
}
public void setClients(List<Client> clients) {
this.clients = clients;
}
...
}
After doing some research it looks like the only solution is for mapping a join table with additional columns using @OneToMany
@IdClass
and a composite primary key class when I don't need additional columns. Is this the only solution besides the one I've included in the code above, which is using a @UniqueConstraint
with the two foreign key columns on the @ManyToMany
mapping? It seems a bit ridiculous the amount of work needed for a common scenario like this. Thanks!
I had a similar problem. All I did was that I changed the collection type from List to Set:
private List<InstrumentTraded> instrumentsTraded;
to
private Set<InstrumentTraded> instrumentsTraded;
And somehow Hibernate now generates primary keys for the join table.
Here's a way to solve the problem:
Client.java:
@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinTable(
joinColumns = {@JoinColumn(name = "client_id")},
inverseJoinColumns = {@JoinColumn(name = "instrument_traded_id")},
uniqueConstraints = {@UniqueConstraint(
columnNames = {"client_id", "instrument_traded_id"})}
)
private List<InstrumentTraded> instrumentsTraded;
This is for unidirectional mapping. If you want a bidirectional relationship, change the mapping in InstrumentTraded.class
to the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With