Cascade

MySql和Hibernate中关于cascade的用法

社会主义新天地 提交于 2019-11-28 23:51:54
数据库里的cascade的用法,Mysql和Hibernate里面是不相同。 在数据库里,进行增加、修改、删除记录的时候,经常会涉及到父子关系的表。 例如:有省份表和城市表,其中城市表有一个外键province_id引用到省份表的主键。这样,可以把省份表看成是父表,把城市表看成是子表,城市表记录的存在依赖于省份表的记录。(文中提到的例子,所有的代码在附件里都有,所以这里的描述从简) 一、在MySQL里的cascade 以下直接在MySQL的控制台操作省份表和城市表 在省份表增加一条“广东”的记录,在城市表增加一条“广州”的记录,并且把“广州”的外键引用到“广东”的主键。“广州”的存在依赖于“广东”,如果删除省份表的“广东”,将会影响到城市表的“广州”。根据城市表的外键约束的on delete设置,有如下三种情况: 1、外键没有on delete的设置:当删除“广东”的时候,MySQL会报错,删除失败。 2、外键设置为on delete cascade:当删除“广东”的时候,同时把“广州”也删除。 3、外键设置为on delete set null:当删除“广东”的时候,“广州”的外键province_id会被自动设置为null,即“广州”脱离了对“广东”的依赖关系。 二、在Hibernate里的cascade 以下用Hibernate来操作省份表和城市表 首先,在hibernate

Nhibernate Cascade

孤者浪人 提交于 2019-11-28 18:19:09
What does Cascade in Nhibernate mean? I see a lot of options in cascading: Delete All AllDeleteOrphan DeleteOrphan SaveUpdate Can you explain these with with examples and their distinctions? Mitch Wheat It means apply the action to an item's related items. Please see: NHibernate Cascades: the different between all, all-delete-orphans and save-update : none - do not do any cascades, let users handle them by themselves. save-update - when the object is saved/updated, check the associations and save/update any object that requires it (including save/update the associations in many-to-many

Introducing FOREIGN KEY constraint 'c_name' on table 't_name' may cause cycles or multiple cascade paths

╄→尐↘猪︶ㄣ 提交于 2019-11-28 13:15:27
问题 I have a database table called Lesson : columns: [LessonID, LessonNumber, Description] ...plus some other columns I have another table called Lesson_ScoreBasedSelection : columns: [LessonID,NextLessonID_1,NextLessonID_2,NextLessonID_3] When a lesson is completed, its LessonID is looked up in the Lesson_ScoreBasedSelection table to get the three possible next lessons, each of which are associated with a particular range of scores. If the score was 0-33, the LessonID stored in NextLessonID_1

Cascade Delete Rule in EF 4.1 Code First when using Shared Primary Key Association

你离开我真会死。 提交于 2019-11-28 10:16:34
I implemented a bidirectional 1:1 relationship based on this answer: Primary /Foreign Key in Entity Framework I define the bidirectional relation this way: public class Student { public virtual int StudentId { get; set; } public virtual Anamnesis Anamnesis { get; set; } . . . } public class Anamnesis { [Key, ForeignKey("Student")] public int AnamnesisId { get; set; } public virtual Student Student { get; set; } . . . } where, Student is the principal entity and Anamnesis it the entity that shares the PK. Now I'd like that the relationship created had a Delete Rule = CASCADE. Actually, the

CASCADE Delete in many-to-many self-reference table

大憨熊 提交于 2019-11-28 08:18:51
问题 Table DISPLAY_TAB below is a self-reference table that can contain both parent and child tabs. A parent tab can have multiple child tabs and a child tab can belong to multiple parents. I'd like to establish a CASCADE DELETE relationship between main table and relationship table DISPLAY_TAB_GROUPING so when either parent or child tab is deleted - relationship is automatically deleted as well (just relationship, not actual tab record). So I am creating a FOREIGN KEY constrain on DISPLAY_TAB

MySQL RESTRICT and NO ACTION

隐身守侯 提交于 2019-11-28 07:55:15
What's the difference in a MySQL FK between RESTRICT and NO ACTION ? From the doc they seem exactly the same. Is this the case? If so, why have both? From MySQL Documentation: https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT . They are identical in MySQL. In the SQL 2003 standard there are 5 different referential actions: CASCADE RESTRICT NO ACTION SET NULL SET DEFAULT The difference between NO

how to define an inverse cascade delete on a many-to-one mapping in hibernate

为君一笑 提交于 2019-11-28 07:32:58
I have two classes A and B. Many B's can have association with a single A, hence a many-to-one relationship from B to A. I've mapped the relationship like: <class name="A" table="tbl_A"> <property name="propA" column="colA"/> </class> <class name="B" table="tbl_B"> <property name="propB" column="colB"/> <many-to-one name="a" class="A" column="col1" cascade="delete"/> </class> A has nothing mapped to B. Keeping this in mind we intend to delete B when it's associated A is deleted. This could have been possible if I could define an inverse="true" on the many-to-one association in B but hibernate

Hibernate Delete Cascade

拜拜、爱过 提交于 2019-11-28 03:17:59
问题 I Have one entity [Project] that contains a collection of other entities [Questions]. I have mapped the relation with a cascade attribute of "all-delete-orphan". In my DB the relation is mapped with a project_id (FK) field on the questions table. this field cannot be null since I don't want a Question without a Project. When I do session.delete(project) it throws an exception saying that project_id cant be null, but if I remove the not-null constrain to that field, the deletion works nice.

Detached entity passed to persist when save the child data

落花浮王杯 提交于 2019-11-27 21:22:32
I'm getting this error when submitting the form: org.hibernate.PersistentObjectException: detached entity passed to persist: com.project.pmet.model.Account; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.project.pmet.model.Account Here are my entities: Account: @Entity @DynamicInsert @DynamicUpdate public class Account { @Id @GeneratedValue private Integer id; @Column(nullable = false) private String login; @Column(nullable = false) private String password; @Column(nullable = false) private String

How does WillCascadeOnDelete in Entity Framework work?

為{幸葍}努か 提交于 2019-11-27 17:44:25
问题 As I understand, if I delete a parent row, its children should be deleted if I turn on cascade on delete. However, from my testing, it doesn't seem to work at all. No matter if I set WillCascaseOnDelete to true or false, it simply sets the foreign key of its children to null. This is causing another problem that I have to set the foreign key nullable, otherwise, SaveChange will throw exception. Is this a defect or desired behavior? 回答1: This is because your foreign keys (child) are nullable.