Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Jacques Yang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Employee.java 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) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

mapping -- User.hbm.xml

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

mapping -- UserInf.hbm.xml

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

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.
Thanks
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!