如何在两个外键上设置Hibernate@ManyToMany与级联的关联?
我正在尝试使用hibernate映射@ManyToMany关联。但到目前为止,我只在其中一个外键上进行了级联 我的源代码如下所示:如何在两个外键上设置Hibernate@ManyToMany与级联的关联?,hibernate,orm,foreign-keys,cascade,many-to-many,Hibernate,Orm,Foreign Keys,Cascade,Many To Many,我正在尝试使用hibernate映射@ManyToMany关联。但到目前为止,我只在其中一个外键上进行了级联 我的源代码如下所示: @Entity public class Airplane { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @OnDelete(action=OnDeleteAction.CASCADE) @ManyToMany(mappedBy="a
@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;
...
}
不知何故,hibernate放弃了乘客类上的@OnDelete(action=OnDeleteAction.CASCADE)注释。Hmm,不确定这是否有效。我的眼睛很老
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);