gpt4 book ai didi

java - "Repeated column in mapping for collection"用于具有相同外键的 hibernate 多对多?

转载 作者:行者123 更新时间:2023-12-02 07:12:18 26 4
gpt4 key购买 nike

我有如下 4 个表:

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)
);

如您所见,market 是 OneToMany 到 market_channel_group、market_channel。market_channel_group是特定市场下的ManyToMany到market_channel。

问题出在我的 market_channel_group 实体中,我有以下代码:

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

...
}

但是,我遇到了这个异常:

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

如何正确编写ManyToMany注解?还是我的设计有问题。

更新

由于我们将从外部资源导入数据,因此自增id不会用作其他表的引用,每个表除了自己的id之外还有另一个索引。

最佳答案

对我来说,通过将 insertable = false, updatable = false 添加到 @JoinColumn 来解决这个问题。

关于java - "Repeated column in mapping for collection"用于具有相同外键的 hibernate 多对多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39767796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com