JPA + 休眠:如何定义具有 ON DELETE 级联的约束
我只是想知道是否有这样一种方法可以让我的MySQL表作为
ALTER TABLE `USERINFO`
ADD CONSTRAINT `FK_USER_ID` FOREIGN KEY (`USERID`) REFERENCES `USERACCOUNT` (`USERID`)
ON DELETE CASCADE
ON UPDATE CASCADE;
但是,只有当休眠 ++ jpa 开始构建具有 ”<property name="hibernate.hbm2ddl.auto" value="create" />
"
ALTER TABLE `USERINFO` ADD CONSTRAINT `FK_USER_ID` FOREIGN KEY (`USERID`) REFERENCES `USERACCOUNT` (`USERID`);
在我的课堂上,我有这些注释设置,
// 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;
}
...
和
// 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;
}
...
请注意,UserAccount 表是此处的父表/主表,而 UserInfo 是规范化为另一个实体的扩展表。任何答案将不胜感激。我只是好奇它是如何完成的,因为我也喜欢在MySQL中工作。我只是非常习惯于从父表(USERACOUNT)中删除记录,这也将允许我根据父表/主表中的特定记录级联删除子记录。
谢谢!