-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to setup Hibernate @ManyToMany association with cascade?
PostPosted: Tue Jan 26, 2010 4:40 am 
Newbie

Joined: Thu Dec 17, 2009 2:46 pm
Posts: 5
I'm trying to map a @ManyToMany association using hibernate. I want to have cascades on both foreign keys. 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);

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


Top
 Profile  
 
 Post subject: Re: How to setup Hibernate @ManyToMany association with cascade?
PostPosted: Wed Jan 27, 2010 7:17 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
There is problem in your mapping.
Please visit this site .
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

if you still have problem with code. I can help you write the code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.