我只是想知道是否有这样的方式可以将我的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的约束
发布评论