Reputation: 1894
I have an unidirectional OneToMany JPA entity mapping in my (Spring Framework + Spring Data + Hibernate JPA) project. Entity classes are like in the following code.(I have removed irrelevant class members for brevity).
@Entity
@Table(name = "employees")
class Employee{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "employee_id")
private List<DepartmentAssignment> departmentAssignments = new ArrayList<>();
}
@Entity
@Table(name = "department_assignments")
class DepartmentAssignment{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@NotNull
@Column(name = "employee_id")
private Integer employeeId;
@NotNull
@Column(name = "department_id")
private Integer departmentId;
}
@Entity
@Table(name = "departments")
class Department{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
}
And, in one of my service classes have a method to remove a DepartmentAssignment from an Employee like below.
public Employee deleteDepartmentAssignment(Integer empId, Integer deptAssignmentId) {
Employee employee = employeeRepository.findOne(empId);
if(employee != null) {
for ( DepartmentAssignment da : employee.getDepartmentAssignments()) {
if(da.getId().equals(deptAssignmentId)) {
employee.getDepartmentAssignments().remove(da);
employee = employeeRepository.save(employee);
break;
}
}
}
return employee;
}
However, calling above methods gives me an error: org.hibernate.exception.ConstraintViolationException
,and in the SQL log, I can see Column 'employee_id' cannot be null
error for the last SQL statement of the transaction.
Can anybody tell me what I'm doing wrong here and how to get it fixed?
Upvotes: 0
Views: 818
Reputation: 5503
You can try the following, not sure why you use the plain id in the object. Thats not object relational mapping. For more details see Hibernate triggering constraint violations using orphanRemoval
@Entity
@Table(name = "employees")
class Employee{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "employee", orphanRemoval = true)
private List<DepartmentAssignment> departmentAssignments = new ArrayList<>();
}
@Entity
@Table(name = "department_assignments")
class DepartmentAssignment{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(optional=false)
private Employee employee;
@ManyToOne(optional=false)
private Department department;
}
@Entity
@Table(name = "departments")
class Department{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
}
Upvotes: 1
Reputation: 19956
You don't need to add
@NotNull
@Column(name = "employee_id")
private Integer employeeId;
to the Employee
, if you use @JoinColumn(name = "employee_id")
. Try to remove it.
Upvotes: 1
Reputation: 733
You must look .hbm.xml file and you should mapping your Entity in this file and you can look this example
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/
I hope it will be useful for you.
Upvotes: 0