Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Repeated column in mapping for collection" for hibernate ManyToMany with same foreign key?

I have 4 tables as following:

create table market (
    id int(6) unsigned auto_increment primary key,
    market_id varchar(30) unique
);

create table market_channel_group (
    id int(6) unsigned auto_increment primary key,
    market_id varchar(30),
    channel_group_id varchar(30),
    unique index (market_id,channel_group_id)
);

create table market_channel (
    id int(6) unsigned auto_increment primary key,
    market_id varchar(30),
    channel_id varchar(30),
    unique index (market_id,channel_group_id)
);

create table market_channel_group_detail (
    id int(6) unsigned auto_increment primary key,
    market_id varchar(30),
    channel_group_id varchar(30),
    channel_id varchar(30),
    unique index (market_id,channel_group_id, channel_id)
);

as you can see, market is OneToMany to market_channel_group, market_channel. market_channel_group is ManyToMany to market_channel under specific market.

The problem is in my market_channel_group entity, I have following code:

@Entity(name = "market_channel_group")
@Table(name = "market_channel_group", uniqueConstraints = {@UniqueConstraint(columnNames = {"market_id", "channel_group_id"})})
public class MarketChannelGroup extends BaseEntity {

    private static final long serialVersionUID = 1L;

    public MarketChannelGroup() {
        super();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "market_id", referencedColumnName = "market_id")
    private Market market;

    @Column(name = "channel_group_id", length = 50)
    private String channelGroupId;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "market_channel_group_detail",
            joinColumns = {@JoinColumn(name = "market_id", referencedColumnName = "market_id"), @JoinColumn(name = "channel_group_id", referencedColumnName = "channel_group_id")},
            inverseJoinColumns = {@JoinColumn(name = "market_id", referencedColumnName = "market_id"), @JoinColumn(name = "channel_id", referencedColumnName = "channel_id")})
    private List<MarketChannel> channels;

...
}

However, I got this exception:

Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: MarketChannelGroup.channels column: market_id
    at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:343) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
...

How should I write the ManyToMany annotation correctly? or is there any problem of my design.

UPDATE

since we will import data from external resource, the auto increment id is not used as other tables' reference, each table has another index other than its own id.

like image 274
seaguest Avatar asked Nov 09 '22 08:11

seaguest


1 Answers

For me, it was solved by adding insertable = false, updatable = false to the the @JoinColumn.

like image 71
Vojtěch Avatar answered Nov 14 '22 22:11

Vojtěch