-->
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.  [ 6 posts ] 
Author Message
 Post subject: SchemaExport not generating CASCADE's with Mysql5Innodb
PostPosted: Mon Mar 12, 2007 7:03 pm 
Newbie

Joined: Thu Nov 30, 2006 6:09 pm
Posts: 9
SchemaExport is not adding 'CASCADE ON ..." when it creates foreign keys during the schema creation. I have a feeling that this is specific to using org.hibernate.dialect.MySQL5InnoDBDialect and the translation between the JPA annotations and the SQL.

With hibernate.hbm2ddl.auto set to 'create' I get the SQL shown in the log below. From what I understand, both foreign key constraints should have 'on delete cascade' at the end of them.

Hibernate Core version: 3.2.2
Hibernate Annotations version: 3.2.1
Hibernate Entity Manager version: 3.2.1

Name and version of the database you are using: MySQL 5.0 InnoDB

The generated SQL (show_sql=true):
15:54:05,387 DEBUG SchemaExport:303 - create table FirstTable (id integer not null auto_increment, primary key (id)) ENGINE=InnoDB
15:54:05,507 DEBUG SchemaExport:303 - create table FirstTable_SecondTable (FirstTable_id integer not null, seconds_id integer not null) ENGINE=InnoDB
15:54:05,627 DEBUG SchemaExport:303 - create table SecondTable (id integer not null auto_increment, primary key (id)) ENGINE=InnoDB
15:54:05,747 DEBUG SchemaExport:303 - alter table FirstTable_SecondTable add index FK95CB7BF97BA45F79 (FirstTable_id), add constraint FK95CB7BF97BA45F79 foreign key (FirstTable_id) references FirstTable (id)
15:54:06,038 DEBUG SchemaExport:303 - alter table FirstTable_SecondTable add index FK95CB7BF91A00BD6 (seconds_id), add constraint FK95CB7BF91A00BD6 foreign key (seconds_id) references SecondTable (id)
15:54:06,298 INFO SchemaExport:196 - schema export complete

The java files:
Code:
@Entity
public class FirstTable
{
   
   @Id
   @GeneratedValue
   private int id;
   
   @ManyToMany(cascade=CascadeType.REMOVE)
   List<SecondTable> seconds;
   
}

Code:
@Entity
public class SecondTable
{
   
   @Id
   @GeneratedValue
   private int id;
   
}


This does appear to be a bug to me, but I'd like to get some feedback before I file it on JIRA. Any comments/questions are appreciated!

Thanks,
Jim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 6:48 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you probably need to use @OnDelete

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 6:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
and no this is not a bug :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 7:03 pm 
Newbie

Joined: Thu Nov 30, 2006 6:09 pm
Posts: 9
emmanuel wrote:
you probably need to use @OnDelete


Using:
@OnDelete(action=OnDeleteAction.CASCADE)

I get:
org.hibernate.MappingException: only inverse one-to-many associations may use on-delete="cascade":

Apparently this does not work for many-to-many associations. When I delete a row from FirstTable, I would like any rows in FirstTable_SecondTable that reference that row to be deleted. How should I approach this? Do I need explicit code for the link table?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 7:13 pm 
Newbie

Joined: Thu Nov 30, 2006 6:09 pm
Posts: 9
emmanuel wrote:
and no this is not a bug :-)


It looks like I have some more reading to do... I just found a note about cascading and ManyToMany on page 24 of the EJB persistence spec, it's a no-no.

What do you think about EntityManager throwing an exception when a cascade annotation is applied to a ManyToMany, instead of just ignoring it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 8:25 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
cascading as defined in the spec has nothing to do with FK cascading, the spec does not define FK cascading actually.

Your problem is different.
A logical @manuToMany cannot combine with @OnDelete, it does not make much sense since other entities might be associated to the target entity

But a
@OneToMany @JoinTable could
Unfortunately, I think Hibernate core will still complain.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.