休眠不会产生级联 [英] Hibernate doesn't generate cascade

查看:135
本文介绍了休眠不会产生级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套 hibernate.hbm2ddl.auto 来创建,以便Hibernate为我创建mysql中的表。



但是,hibernate似乎并没有在表中的引用上正确添加 Cascade 然而,当我例如删除一行时,它有效,并且我有一个删除级联作为hibernate注释。所以我想这意味着Hibernate会在运行时读取annoation,并手动执行级联操作?



这是正常行为吗?



例如:

  @Entity 
class Report {
@OneToOne(cascade = CascadeType。 ALL)
public File getPdf(){
return pdf;




$ b $ p
$ b $ p $在这里我已经设置级联到ALL。但是,运行 show create table时

 报告| CREATE TABLE`Report`(
`id` bigint(20)NOT NULL AUTO_INCREMENT,
`pdf_id` bigint(20)DEFAULT NULL,
PRIMARY KEY(`id`),
KEY'FK91B14154FDE6543A'(`pdf_id`),
CONSTRAINT`FK91B14154FDE6543A` FOREIGN KEY(`pdf_id`)REFERENCES`File`(`id`)
)ENGINE = InnoDB DEFAULT CHARSET = utf8 |

它没有提到任何关于级联其他外键的事情。在我看来,它应该添加 ON DELETE CASCADE ON DELETE UPDATE

解决方案

CascadeType @Cascade 定义不是转换成DDL,它们告诉Hibernate在执行操作时的行为方式。



然而, @OnDelete ,您可以在父级上使用它来获取创建的外键用适当的删除级联条款。


I have a set hibernate.hbm2ddl.auto to create so that Hibernate creates the tables in mysql for me.

However, it doesn't seem that hibernate correctly adds Cascade on the references in the table. It does however work when I for instance delete a row, and I have a delete cascade as hibernate annotation. So I guess that means that Hibernate reads the annoation on runtime, and perform cascading manually?

Is that normal behavior?

For instance:

@Entity
class Report {
    @OneToOne(cascade = CascadeType.ALL)
    public File getPdf() {
    return pdf;
}
}

Here I have set cascade to ALL. However, when running show create table Report

Report | CREATE TABLE `Report` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `pdf_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK91B14154FDE6543A` (`pdf_id`),
  CONSTRAINT `FK91B14154FDE6543A` FOREIGN KEY (`pdf_id`) REFERENCES `File` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

It doesn't say anything about cascading other then the foreign key. In my opinion, it should have added the ON DELETE CASCADE ON DELETE UPDATE

解决方案

The CascadeType and @Cascade definitions are not translated into DDL, they tell Hibernate how to behave when an operation is performed.

However, there is @OnDelete that you can use on the parent to get the foreign key created with the appropriate on delete cascade clause.

这篇关于休眠不会产生级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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