@OnDelete休眠注释不会为MySql生成ON DELETE CASCADE [英] @OnDelete Hibernate annotation does not generate ON DELETE CASCADE for MySql

查看:75
本文介绍了@OnDelete休眠注释不会为MySql生成ON DELETE CASCADE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有单向关系的父级和子级

I have two entities parent and child with a unidirectional relationship

class Parent {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
}

class Child {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "parent_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Parent parent;
}

在我配置的application.properties文件中

In the application.properties file I have configured

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.database-platform = org.hibernate.dialect.MySQL5InnoDBDialect

但是当我运行应用程序时,将创建以下语句

But when I run the application the following statement is created

...
alter table child add constraint FKi62eg01ijyk2kya7eil2gafmx foreign key (parent_id) references parent (id)
...

因此,没有应有的ON CASCADE DELETE.每次我运行应用程序并检查方法是否创建表时都会

So there is no ON CASCADE DELETE as there should be. The tables are created each time I run the application and I checked if the method

org.hibernate.dialect.MySQL5InnoDBDialect#supportsCascadeDelete()

真的被称为(是).我正在使用spring-boot-parent版本1.4.3,该版本使用Hibernate 5.11.有任何想法吗?我不想顺便使用双向关系.

is really called (it is). I am using spring-boot-parent version 1.4.3, which uses Hibernate 5.11. Any ideas? I do not want to use a bi-directional relationship by the way.

修改 感谢@AlanHay,我发现我省略了重要的部分.实际上涉及到三等班

Edit Thanks to @AlanHay I discovered that I omitted an important part. There actually is a third class involved

class Kindergarten {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy = "kindergarten", fetch = FetchType.EAGER)
    @MapKeyJoinColumn(name = "parent_id") // parent_id causes the problem!
    private Map<Parent, Child> children;
}

带幼儿园的孩子看起来像这样

and Child with Kindergarten looks actually like this

class Child {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "parent_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Parent parent;

    @ManyToOne
    @JoinColumn(name = "kindergarten_id")
    private Kindergarten kindergarten;
}

,这就是问题发生的原因.如果您将MapKeyJoinColumn批注中的"parent_id"更改为Child作为列中不存在的内容,例如将ON DELETE CASCADE的"map_id"添加到Child的外键parent_id中.如果参数是子级的"parent_id"列,则不会附加ON DELETE CASCADE部分.不幸的是,原因尚不清楚.更改参数是没有选择的,因为我想使用到子对象的父对象的现有链接.

and that is why the problem occurs. If you change "parent_id" in the MapKeyJoinColumn annotation to something not existing in Child as a column e.g. "map_id" the ON DELETE CASCADE is added to the foreign key parent_id in Child. If the parameter is the Child's column "parent_id" the ON DELETE CASCADE part will not be appended. Unfortunately the reason for this is not yet clear to me. Changing the parameter is no option because I want to use the existing link to parent of the child object.

推荐答案

也许有点晚,但由于它是搜索休眠ondelete生成级联"时的热门文章之一:

Maybe a little late, but since it is one of the top posts when searching for 'hibernate ondelete generate cascade':

由于某种原因,将@OnDelete放在Mysql的ManyToOne端对我不起作用,但它在OneToMany端起作用.因此,如果您不走运,请尝试另一面.

For some reason putting @OnDelete on the ManyToOne side in Mysql did not work for me, but it worked on the OneToMany side. So if you are unlucky, try it on the other side.

这篇关于@OnDelete休眠注释不会为MySql生成ON DELETE CASCADE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆