Questions tagged [cascade]

Cascade refers to a table-definition keyword in relational databases; it instructs the query engine to take a certain action (delete, update), when a primary key is modified, on tables linked by a foreign key.

Cascade is a keyword that helps define the behavior of database tables linked by a foreign key, when the primary table is changed. If cascade is specified in the table definition, then, for example:

  • if a row in the primary table is deleted, the corresponding rows in the linked table can be deleted
  • if a primary key is updated, then the corresponding rows can be updated
1381 questions
15
votes
5 answers

JPA + Hibernate: How to define a constraint having ON DELETE CASCADE

I am just wondering if there's such a way that I can have build my MySQL table as ALTER TABLE `USERINFO` ADD CONSTRAINT `FK_USER_ID` FOREIGN KEY (`USERID`) REFERENCES `USERACCOUNT` (`USERID`) ON DELETE CASCADE ON UPDATE…
toytoy
  • 1,223
  • 1
  • 12
  • 14
14
votes
2 answers

Confusion between JPA and Hibernate cascading

I'm using Hibernate 3.6 and have my code annotated (versus using hibernate mapping files). I ran into the known "problem" of using JPA cascading options that are not compatible with Hibernate's CascadeType (see this link for more info…
Gordon
  • 1,210
  • 5
  • 16
  • 23
14
votes
1 answer

How do I change a child's parent in NHibernate when cascade is delete-all-orphan?

I have two entities in a bi-directional one-to-many relationship: public class Storage { public IList Boxes { get; set; } } public class Box { public Storage CurrentStorage { get; set; } } And the mapping:
Daniel T.
  • 37,212
  • 36
  • 139
  • 206
13
votes
2 answers

Hibernate: How to use cascade in annotation?

How can I use cascade and annotations in hibernate? But I stay with a doubt: I have this situation: public class Package(){ @OneToOne(cascade=CascadeType.PERSIST) private Product product; @OneToOne(cascade=CascadeType.PERSIST) private User…
Valter Silva
  • 16,446
  • 52
  • 137
  • 218
13
votes
1 answer

Entity Framework : Why WillCascadeOnDelete() Method is ignored?

Here is my situation : public abstract class Article { [key] public Guid Guid { get; set;} public string Name { get; set;} . . . } public class Download : Article { ... } public abstract class Category : Article { …
Manoochehr Dadashi
  • 715
  • 1
  • 10
  • 28
13
votes
3 answers

How to CASCADE a delete from a child table to the parent table?

I prepared a fiddle which demonstrates the problem. CREATE TABLE parent ( parent_id integer primary key ); CREATE TABLE child ( child_name TEXT primary key, parent_id integer REFERENCES parent (parent_id) ON DELETE CASCADE ); INSERT INTO…
samol
  • 18,950
  • 32
  • 88
  • 127
13
votes
1 answer

Hibernate : CascadeType.PERSIST does not work but CascadeType.ALL to save object

@Entity @Table(name = "Section_INST") public class Section { @javax.persistence.Id @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "Section_ID_GENERATOR") @SequenceGenerator(name = "Section_ID_GENERATOR",sequenceName =…
HakunaMatata
  • 814
  • 2
  • 12
  • 22
13
votes
4 answers

MySQL ON UPDATE CASCADE not CASCADEing

Suppose i have two below table: CREATE TABLE post ( id bigint(20) NOT NULL AUTO_INCREMENT, text text , PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=1; CREATE TABLE post_path ( ancestorid bigint(20) NOT NULL DEFAULT '0', …
ahoo
  • 1,321
  • 2
  • 17
  • 37
13
votes
1 answer

Many to Many: Delete one side, the relationship entry BUT don't delete the other side

I want to delete an user which has many usergroups but those usergroups don't belong exclusivly to this user: other users can also be using this usergroups. And usergroups can exist even if no user references them. I want to map the many-to-many…
David Rettenbacher
  • 5,088
  • 2
  • 36
  • 45
13
votes
3 answers

Does CASCADE Delete execute as transaction?

I want to perform cascade delete for some tables in my database, but I'm interested in what happens in case there's a failure when deleting something. Will everything rollback?
kjv
  • 11,047
  • 34
  • 101
  • 140
12
votes
1 answer

Does cascade="all-delete-orphan" have any meaning in a Hibernate unidirectional many-to-many association with a join table?

I have two objects which form a parent-child relationship which have a many-to-many relationship. Following the recommendations in the Hibernate reference manual, I have mapped this using a join table:
matt b
  • 138,234
  • 66
  • 282
  • 345
12
votes
3 answers

What are the Pros and Cons of Cascading delete and updates?

Maybe this is sort of a naive question...but I think that we should always have cascading deletes and updates. But I wanted to know are there problems with it and when should we should not do it? I really can't think of a case right now where you…
Vishal
  • 12,133
  • 17
  • 82
  • 128
12
votes
2 answers

MongoDB DBRef ON DELETE CASCADE

Is there a way in MongoDB to have a foreign key with a 'ON DELETE CASCADE' functionality? I know you can use DBRef as a sort of foreign key but when the item in a collection where the reference points to is removed, the reference returns null. But i…
VDVLeon
  • 1,393
  • 1
  • 15
  • 26
12
votes
2 answers

Order of GORM cascaded saves depending on field names?

I have the following arrangement of classes: class A { static belongsTo = [c: C] B b } class B { static belongsTo = [c: C] } class C { static hasMany = [bbs: B, aas: A] } If I now create instances of these classes... B b = new…
jack_kerouac
  • 1,482
  • 2
  • 15
  • 30
12
votes
1 answer

CASCADE DELETE on two foreign key constraints

I have the following example: Table A -some_id Table B -another_id Table C -some_id_fk -another_id_fk I want to cascade a row on Table C if both some_id and another_id are deleted from their respective tables. How can I make a row in…
12preschph
  • 311
  • 1
  • 4
  • 11