Full Exception:
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.gr8riaz.hrms.JobTitle; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.gr8riaz.hrms.JobTitle
This exception occurs when you try to save an object with NULL parent ID. This also occurs when you try to save an object with parent ID which does not exist in database e,g. if you try to save record with parent ID "-1" hibernate session will through an exception mentioned above.
Explaination:
For example if you have following two objects (POJOs):
JobTitle.java
/**
* File Name: JobTitle.java
* Created By: Muhammad Riaz
*/
package com.gr8riaz.hrms;
/**
*
* @author Muhammad Riaz
*
*/
public class JobTitle {
private Long jobTitleId;
private String jobTitleName;
private String jobTitleCode;
public Long getJobTitleId() {
return jobTitleId;
}
public void setJobTitleId(Long jobTitleId) {
this.jobTitleId = jobTitleId;
}
public String getJobTitleName() {
return jobTitleName;
}
public void setJobTitleName(String jobTitleName) {
this.jobTitleName = jobTitleName;
}
public String getJobTitleCode() {
return jobTitleCode;
}
public void setJobTitleCode(String jobTitleCode) {
this.jobTitleCode = jobTitleCode;
}
}
|
Employee.java
/**
* File Name: Employee.java
* Created By: Muhammad Riaz
*/
package com.gr8riaz.hrms;
/**
*
* @author Muhammad Riaz
*
*/
public class Employee {
private Long employeeId;
private String employeeName;
private JobTitle jobTitle;
public JobTitle getJobTitle() {
return jobTitle;
}
public void setJobTitle(JobTitle jobTitle) {
this.jobTitle = jobTitle;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
}
|
In this case if you try to save employee as follows:
............
Employee employee = new Employee();
employee.setEmployeeName("Muhammad Riaz");
employee.setJobTitle(new JobTitle());
hibernateSession.saveOrUpdate(employee);
............
This will give following exception:
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.gr8riaz.hrms.JobTitle; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.gr8riaz.hrms.JobTitle
SOLUTION:
//In case JobTitle is nullable
.............................
Employee employee = new Employee();
employee.setEmployeeName("Muhammad Riaz");
employee.setJobTitle(null); //In case JobTitle is nullable
hibernateSession.saveOrUpdate(employee);
...........................
//In case JobTitle is not nullable
.............................
Employee employee = new Employee();
employee.setEmployeeName("Muhammad Riaz");
JobTitle jobTitle = new JobTitle();
jobTitle.setJobTitleId(new Long(1)); //There should have to be a record of JobTitle with Id=1 in
//database, otherwise it will give the same above exception
employee.setJobTitle(jobTitle);
hibernateSession.saveOrUpdate(employee);
...........................