Hi,
I am using Hibernate core 3.2.6 and hibernate annotations 3.3.0 with spring and struts and MySQL 5.0 . I have problem with ManyToOne annotation. If I have for example table which looks like the one below... and I want to delete Employee with id=4 I cant do it...cuz this row is set as foreign key in the row with id=7. If I change mapping to CascadeType.ALL it will delete row with id=4 and row with id=7. How to do it to set up mapping to delete row with id=4 and set null in supervisor field in row with id=7. I hope I wrote this clear for everybody. I will paste below the exception which hibernate throws and my mapped object. I will be gratefull for any tips.
+----+-----------+----------+--------+-------+------------+
| id | firstname | lastname | mail | nsnId | supervisor |
+----+-----------+----------+--------+-------+------------+
| 4 | Anna | Lub | mail | nsnid | NULL |
| 7 | julia | orka | dsakjh | dskah | 4 |
+----+-----------+----------+--------+-------+------------+
Code:
Hibernate operation: Could not execute JDBC batch update; SQL [delete from employee where id=?]; Cannot delete or update a parent row: a foreign key constraint fails (`nsn_db/employee`, CONSTRAINT `FK4722E6AE10BF6C0` FOREIGN KEY (`supervisor`) REFERENCES `employee` (`id`)); nested exception is java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`nsn_db/employee`, CONSTRAINT `FK4722E6AE10BF6C0` FOREIGN KEY (`supervisor`) REFERENCES `employee` (`id`))
Code:
package nsn.equipment.model;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "employee")
public class Employee {
private Integer _id;
private String _firstname;
private String _lastname;
@SuppressWarnings("unused")
private String _fullname;
private Employee _supervisor;
private String _nsnId;
private String mail;
private String domainLogin;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public Integer getId() {
return _id;
}
public void setId(Integer id) {
_id = id;
}
@Basic
@Column(name = "firstname")
public String getFirstname() {
return _firstname;
}
public void setFirstname(String firstname) {
_firstname = firstname;
}
@Basic
@Column(name = "lastname")
public String getLastname() {
return _lastname;
}
public void setLastname(String lastname) {
_lastname = lastname;
}
@Transient
public String getFullname() {
return _firstname + " " + _lastname;
}
public void setFullname(String fullname) {
_fullname = fullname;
}
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "supervisor",nullable=true)
public Employee getSupervisor() {
return _supervisor;
}
public void setSupervisor(Employee supervisor) {
_supervisor = supervisor;
}
@Basic
@Column(name = "nsnId")
public String getNsnId() {
return _nsnId;
}
public void setNsnId(String nsnId) {
_nsnId = nsnId;
}
@Basic
@Column(name = "mail")
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
@Transient
public String getDomainLogin() {
return domainLogin;
}
public void setDomainLogin(String domainLogin) {
this.domainLogin = domainLogin;
}
}
[/code]