开发者

How to setup Hibernate @ManyToMany association with cascades on both foreign keys?

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})
   开发者_StackOverflow社区 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜