JPA + Hibernate:如何定义一个具有ON DELETE CASCADE的约束

编程入门 行业动态 更新时间:2024-10-25 04:24:59
本文介绍了JPA + Hibernate:如何定义一个具有ON DELETE CASCADE的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是想知道是否有这样的方式可以将我的MySQL表构建为

ALTER TABLE`USERINFO` ADD CONSTRAINT`FK_USER_ID` FOREIGN KEY(`USERID`)REFERENCES`USERACCOUNT`(`USERID`) ON DELETE CASCADE ON UPDATE CASCADE;

然而,我只在DDL中得到这个,当hibernate ++ jpa开始构建我的表时, < property name =hibernate.hbm2ddl.autovalue =create/>

ALTER TABLE'USERINFO'ADD CONSTRAINT'FK_USER_ID` FOREIGN KEY(`USERID`)REFERENCES`USERACCOUNT`(`USERID`);

在我的课程中,我有这些注释设置,

// UserAcc.java @Entity @Table(name =USERACC) public class UserAcc实现Serializable { private static final long serialVersionUID = -5527566248002296042L; @Id @Column(name =USERID) @GeneratedValue(strategy = GenerationType.AUTO) private Integer userId; @OneToOne(mappedBy =userAcc) private UserInfo userInfo; .... public UserInfo getUserInfo(){ return userInfo; } public void setUserInfo(UserInfo userInfo){ this.userInfo = userInfo; } ...

// UserInfo.java @Entity @Table(name =USERINFO) public class UserInfo实现Serializable { private static final long serialVersionUID = 5924361831551833717L; @Id @Column(name =USERINFO_ID,nullable = false) @GeneratedValue(strategy = GenerationType.AUTO) private Integer userInfoId; @OneToOne(cascade = {CascadeType.ALL}) @JoinColumn(name =USERID,nullable = false) @ForeignKey(name =FK_USER_ID) private UserAcc userAcc; public Integer getUserInfoId(){ return userInfoId; } public void setUserInfoId(Integer userInfoId){ this.userInfoId = userInfoId; $ b注意,UserAccount表是父/主表,而UserInfo是一个扩展表,规范化为另一个实体。任何答案将不胜感激。我只是好奇它是如何完成的,因为我喜欢在MySQL中工作。我真的很习惯从父表(USERACOUNT)中删除一条记录,这将允许我通过父级/主表中的特定记录级联删除通过子记录。

谢谢!

解决方案

JPA提供了级联操作(合并,保留,刷新,删除)到关联的实体。逻辑在JPA中,不使用数据库级联。

@OneToMany(cascade = CascadeType.REMOVE) code>

没有符合JPA标准的方式来执行数据库级联级联。如果这样的级联是首选,我们必须回到Hibernate特定的结构: @OnDelete 。它至少与@OneToMany一起工作,但过去曾出现过一些与@OneToOne和@OnDelete有关的问题。

@ OnDelete(action = OnDeleteAction.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 CASCADE;

However, I only got this in my DDL when hibernate ++ jpa starts to build my table having "<property name="hibernate.hbm2ddl.auto" value="create" />"

ALTER TABLE `USERINFO` ADD CONSTRAINT `FK_USER_ID` FOREIGN KEY (`USERID`) REFERENCES `USERACCOUNT` (`USERID`);

In my classes, I have these annotation setup,

// UserAcc.java @Entity @Table(name = "USERACC") public class UserAcc implements Serializable { private static final long serialVersionUID = -5527566248002296042L; @Id @Column(name = "USERID") @GeneratedValue(strategy=GenerationType.AUTO) private Integer userId; @OneToOne(mappedBy = "userAcc") private UserInfo userInfo; .... public UserInfo getUserInfo() { return userInfo; } public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } ...

and,

// UserInfo.java @Entity @Table(name = "USERINFO") public class UserInfo implements Serializable { private static final long serialVersionUID = 5924361831551833717L; @Id @Column(name = "USERINFO_ID", nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) private Integer userInfoId; @OneToOne(cascade = {CascadeType.ALL}) @JoinColumn(name="USERID", nullable=false) @ForeignKey(name = "FK_USER_ID") private UserAcc userAcc; public Integer getUserInfoId() { return userInfoId; } public void setUserInfoId(Integer userInfoId) { this.userInfoId = userInfoId; } ...

Note that, UserAccount table is the parent/main table here while UserInfo is an extended table normalize to another entity. Any answers would be greatly appreciated. I'm just curious how it's done as I love to work also in MySQL. I am just really used to deleting a record from the parent table (USERACOUNT) which would would also allow me to cascade a delete thru child records dependent on the specific record from a parent/primary table.

Thanks!

解决方案

JPA do offer possibility to cascade operations (merge, persist, refresh, remove) to associated entities. Logic is in JPA and does not utilize database cascades.

@OneToMany(cascade=CascadeType.REMOVE)

There is no JPA standard compliant way to do cascades with database cascades. If such a cascades are preferred, we have to fall back to Hibernate specific construct: @OnDelete. It works with @OneToMany at least, but there used to be some problems in the past with @OneToOne and @OnDelete.

@OnDelete(action = OnDeleteAction.CASCADE)

更多推荐

JPA + Hibernate:如何定义一个具有ON DELETE CASCADE的约束

本文发布于:2023-10-25 21:51:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   Hibernate   JPA   DELETE   CASCADE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!