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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate + Mysql error on deleting rows
PostPosted: Sun Feb 03, 2008 7:07 am 
Newbie

Joined: Sun Feb 03, 2008 6:36 am
Posts: 3
Hi, i am using hibernate with MySQL 5.0.20.

I have a BaseBean class that is parent for all of classes i am using, and that class has @Id notation. So basically my classes pickup Id from that class.

I am using MySQL Dialect. When Hibernate create tables and i populate it with data (it works well) and when i try to delete ANYTHING it throws exception

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mobilestock/monitoring_shares`, CONSTRAINT `FKF0ED504B79AC696C` FOREIGN KEY (`MonitoringSystem_ID`) REFERENCES `monitoringsystems` (`_id`))

Even with MySQL GUI i cannot delete any rows with same error -1217.

If i change DDL of tables (manually, after hibernate creates it) and set On Delete and On Update to 'cascade' instead of 'restrict' i can do do delete operations.

What might be the problem? I guess that problem is more on low-level (when creating database and tables, not with hibernate) so my question would be how to make hibernate add this cascade stuff to DDL when creating tables.

I can post my class definitions and hierarchy among them, but i did not put it now because i think it is irritating to see tons of code. I am using only annotations, not hbm.xml's and using Spring as a container


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 03, 2008 7:30 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
I have no experiance with hibernate schema generation, but are you using @Cascade annotations? If not, I suspect that wil solve the problem.


Top
 Profile  
 
 Post subject: Re: Hibernate + Mysql error on deleting rows
PostPosted: Sun Feb 03, 2008 11:42 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
mnovakovic wrote:

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mobilestock/monitoring_shares`, CONSTRAINT `FKF0ED504B79AC696C` FOREIGN KEY (`MonitoringSystem_ID`) REFERENCES `monitoringsystems` (`_id`))

The error message does tells you all. You cannot delete a row from a database table which is referenced in some other table: foreign key constraints are an essentiell feature of relational databases! So this has nothing to do with Hibernate, even if you generated the schema with Hibernate. (As you noticed by using the MySql Admin GUI).

Carlo
-----------------------------------
if this post helped please give me some credits.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 03, 2008 6:14 pm 
Newbie

Joined: Sun Feb 03, 2008 6:36 am
Posts: 3
I did use @Cascade annotations on both @ManyToMany and @ManyToOne columns.

@ Carlof

So basically what would you suggest? How can i make hibernate add that DB cascade options? Or if he cannot add those constraint, how should i delete objects from database? This way it is not possible because DBMS is always giving that error


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 03, 2008 6:33 pm 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
mnovakovic wrote:
I did use @Cascade annotations on both @ManyToMany and @ManyToOne columns.


I doubt hibernate bothers to try to cascade delete on ManyToMany or ManyToOne relationships. How would it know that it's safe to carry out the cascade?

Cascade delete works on OneToMany relationships. Normally, the parent in the relationship has @OneToMany + @Cascade on a Collection of child entities. The cascade delete is obviously then invoked when the parent is deleted.

If you haven't got the cascade configured on the @OneToMany side, I would not expect the generated schema to allow deletion of parent table rows when children exist.

As I said before, I've never used the schema generation before, I'm just going from my experiance of parent-child relationships in Java/Hibernate/Relational Databases.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 03, 2008 7:17 pm 
Newbie

Joined: Sun Feb 03, 2008 6:36 am
Posts: 3
My bad for my answer. I actually did put @Cascade annotation only on @OneToMany side of relation, i just said it wrong for no reason i guess.

I am basically concerned about DDL that hibernate creates. I could not delete any row from MySQL gui because i am getting that error. How can i manipulate this options. Now i have next DDL:

CREATE TABLE `shares` (
`_id` bigint(20) NOT NULL,
`name` varchar(255) default NULL,
`value` double NOT NULL,
PRIMARY KEY (`_id`),
KEY `FK9389989413B8D08` (`_id`),
CONSTRAINT `FK9389989413B8D08` FOREIGN KEY (`_id`) REFERENCES `basebean` (`_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

and i need this one

CREATE TABLE `shares` (
`_id` bigint(20) NOT NULL,
`name` varchar(255) default NULL,
`value` double NOT NULL,
PRIMARY KEY (`_id`),
KEY `FK9389989413B8D08` (`_id`),
CONSTRAINT `shares_ibfk_1` FOREIGN KEY (`_id`) REFERENCES `basebean` (`_id`)

ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Top
 Profile  
 
 Post subject: adding to the soup...
PostPosted: Wed Apr 09, 2008 11:55 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Hi, I have a similar problem. I also noticed that Hibernate doesn't add ON CASCADE to the DDL, but it seems common behavior with other ORMs as well.. it is as if hibernate doesn't "rely" on the fact that the underlying db provides FKs, and chooses to manage it on his own. In my case, I've noticed that a call to:
Code:
entitymanager.remove(parent);

works great, removes all children, etc.
But a query that does the same, i.e.
Code:
delete from Parent p where parentId = 6

causes the exception. Seems like Hibernate doesn't "want" to utilize its relationship knowledge during the DELETE query...

any suggestions? I'm stuck on this one as well...
I'm using Hib 3.2.6 GA.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.