如何在两个外键上设置 Hibernate @ManyToMany 与级联的关联?
Posted
技术标签:
【中文标题】如何在两个外键上设置 Hibernate @ManyToMany 与级联的关联?【英文标题】:How to setup Hibernate @ManyToMany association with cascades on both foreign keys? 【发布时间】:2010-01-26 08:21:18 【问题描述】:我正在尝试使用休眠映射@ManyToMany 关联。但到目前为止,我只设法在其中一个外键上进行级联。
我的源代码是这样的:
@Entity
public class Airplane
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OnDelete(action=OnDeleteAction.CASCADE)
@ManyToMany(mappedBy="airplanes", cascade = CascadeType.ALL)
private Set<Passenger> passengers;
...
@Entity
public class Passenger
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OnDelete(action=OnDeleteAction.CASCADE)
@ManyToMany(cascade = CascadeType.ALL)
private Set<Airplane> airplanes;
...
hibernatetool 输出:
create table Airplane (
id bigint not null auto_increment,
objVersion bigint,
primary key (id)
) type=InnoDB;
create table Passenger (
id bigint not null auto_increment,
objVersion bigint,
primary key (id)
) type=InnoDB;
create table Passenger_Airplane (
passengers_id bigint not null,
airplanes_id bigint not null,
primary key (passengers_id, airplanes_id)
) type=InnoDB;
alter table Passenger_Airplane
add index FKC9262997C1630114 (airplanes_id),
add constraint FKC9262997C1630114
foreign key (airplanes_id)
references Airplane (id)
on delete cascade;
alter table Passenger_Airplane
add index FKC92629979BEE2B2 (passengers_id),
add constraint FKC92629979BEE2B2
foreign key (passengers_id)
references Passenger (id);
不知何故,Passenger 类上的 @OnDelete(action=OnDeleteAction.CASCADE) 注释被休眠丢弃了。
【问题讨论】:
【参考方案1】:嗯,不确定这是否有效。在HHH-4404 中查看非常古老的comment。
【讨论】:
以上是关于如何在两个外键上设置 Hibernate @ManyToMany 与级联的关联?的主要内容,如果未能解决你的问题,请参考以下文章
MySQL 性能:两个表在公共外键上的连接:仍然“使用 where”