如何设置 Hibernate @ManyToMany 与两个外键上的级联关联?

发布于 2024-08-19 00:45:56 字数 1583 浏览 6 评论 0原文

我正在尝试使用 hibernate 映射 @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) 注释被 hibernate 丢弃。

I'm trying to map a @ManyToMany association using hibernate. But so far I only managed to have cascade on one of the foreign keys.

My source code goes like this:

@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 outputs:

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

Somehow the @OnDelete(action=OnDeleteAction.CASCADE) annotation on Passenger class is discarded by hibernate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-08-26 00:45:56

嗯,不确定这是否有效。请参阅非常旧的 评论 in HHH-4404

Hmm, not sure this works. See very old comment in HHH-4404.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文