views:

46

answers:

2

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 (http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#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!

A: 

Your mapping looks odd (in particular the joinColumn part of the @JoinTable annotation). I would expect something like this:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
    joinColumns= 
        @JoinColumn(name="CLIENT_ID", referencedColumnName="ID"), 
    inverseJoinColumns=
        @JoinColumn(name="instrument_traded_id", referencedColumnName="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;

But unless you want to override the defaults (and I guess you do), I would just skip the @JoinTable.

Pascal Thivent
I do want to override the defaults because it pluralizes the the instrument_traded_id (instruments_traded_id). I have a custom naming strategy that adds "_id" if it "_id" doesn't exist on the end of a foreign key. I can remove the joinColumns=@JoinColumn. Do you have a working example of a ManyToMany creating a primary key consisting of two foreign keys in the ManyToMany table?
dukethrash
@dukethrash: `Do you have a working example of a ManyToMany creating a primary key consisting of two foreign keys in the ManyToMany table?` That's the default behavior if properly annotated. Did you try without the `joinColumn` (or with the above one)?
Pascal Thivent
Yes I tried yours (with the exception that referencedColumn is actually referencedColumnName) and it is still not generating the primary keys. I've even added the following on InstrumentTraded just to see if that would change anything. @ManyToMany(fetch = FetchType.LAZY, mappedBy = "instrumentsTraded") private List<Client> clients;
dukethrash
I also tried it without the joinColumn
dukethrash
A: 

Or Split your @ManyToMany into @OneToMany - @ManyToOne relationship See here how you can accomplish this mapping

Arthur Ronald F D Garcia