File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes delete option of cascade in Hibernate one-to-one association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "delete option of cascade in Hibernate one-to-one association" Watch "delete option of cascade in Hibernate one-to-one association" New topic

delete option of cascade in Hibernate one-to-one association

Jacques Yang

Joined: Jul 10, 2012
Posts: 1
I am not able to delete the corresponding one-to-one relation row using delete option of cascade. Here are the code for my class and Mappings.

Code For Class.
public class User {
private Integer uid;
private String username;
private String password;
private UserInf inf;
public UserInf getInf() {
return inf;
public void setInf(UserInf inf) {
this.inf = inf;
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;
public Integer getUid() {
return uid;
public void setUid(Integer uid) {
this.uid = uid;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;

Code for UserInf .java Class

public class UserInf {
private int iid;
private String name;
private String email;
private User user;

public int getIid() {
return iid;
public void setIid(int iid) {
this.iid = iid;
public String getName() {
return name;
public void setName(String name) { = name;
public String getEmail() {
return email;
public void setEmail(String email) { = email;
public User getUser() {
return user;
public void setUser(User user) {
this.user = user;

mapping -- User.hbm.xml

<class name="com.developeryang.domain.User" table="USER">
<id name="uid" type="java.lang.Integer">
<column name="UID" />
<generator class="native" />
<property name="username" type="java.lang.String">
<column name="USERNAME" />
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
<one-to-one name="inf" class="com.developeryang.domain.UserInf" cascade="save-update, delete-orphan"></one-to-one>

mapping -- UserInf.hbm.xml

<class name="com.developeryang.domain.UserInf" table="USERINF">
<id name="iid" type="int">
<column name="IID" />
<generator class="foreign">
<param name="property">user</param>
<property name="name" type="java.lang.String">
<column name="NAME" />
<property name="email" type="java.lang.String">
<column name="EMAIL" />
<one-to-one name="user" class="com.developeryang.domain.User" constrained="true"></one-to-one>

When i delete any row from User table the Corresponding Foreign key row in UserInf Table is not deleting, and it report a ERROR
ERROR: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`userinf`, CONSTRAINT `FK2206A016BD228409` FOREIGN KEY (`IID`) REFERENCES `user` (`UID`))
I don't know why, I already add cascade="save-update, delete-orphan" for User. What is the mistake and When a delete a row from User How could I let hibernate delete the row automatic with same ID
Please Help Me.
jQuery in Action, 2nd edition
subject: delete option of cascade in Hibernate one-to-one association