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!
You can use Hibernate annotation
org.hibernate.annotations@OnDelete(action = OnDeleteAction.CASCADE)
onUserAcc.userInfo
:this will generate DDL
ON DELETE CASCADE
on foreign key inUserInfo
table.JPA does offer possibility to cascade operations (merge, persist, refresh, remove) to associated entities. Logic is in JPA and does not utilize database cascades.
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
.Be aware that adding this annotation to an existing constraint will not update it. You may have to manually drop it first to properly update the schema.
There is no clean cut means to do this in JPA. The following will get you what you want... You can use
CascadeType.DELETE
, however this annotation only applies to the objects in theEntityManager
, not the database. You want to be sure thatON DELETE CASCADE
is added to the database constraint. To verify, you can configure JPA to generate a ddl file. Take a look at theddl
file, you'll notice thatON DELETE CASCADE
is not part of the constraint. AddON DELETE CASCADE
to actual SQL in theddl
file, then update your database schema from the ddl. This will fix your problem .This link shows how to use
ON DELETE CASCADE
on forCONSTRAINT
in MySQL. You do this on the constraint. You can also do it in aCREATE TABLE
orALTER TABLE
statement. It's likely that JPA creates the constraint in anALTER TABLE
statement. Simply addON DELETE CASCADE
to that statement.Note that some JPA implementors do provide a means for this functionality.
Lastly, Hibernate does supply this functionality using the
OnDelete(action = OnDeleteAction.CASCADE)
annotation.