English 中文(简体)
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})
    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.

问题回答

暂无回答




相关问题
Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I m in the process of putting together a simple login module and I m unsure where to put the "business logic." If I put the logic with the data in the ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签