I am getting a strange problem with the merge-method, where a field is set to null for some reason.
My code looks like this:
Code:
public class Project {
[...]
private CaseWorkerA caseWorkerA;
@ManyToOne(fetch = FetchType.EAGER)
@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.PERSIST,
org.hibernate.annotations.CascadeType.REFRESH, org.hibernate.annotations.CascadeType.MERGE })
@JoinColumn(name = "CaseWorker_A")
public CaseWorkerA getCaseWorkerA() {
return caseWorkerA;
}
}
Code:
public class User {
[...]
}
public class CaseWorker extends User {
private CaseWorkerStatus status;
@Enumerated(EnumType.STRING)
public SaksbehandlerStatus getStatus() {
return status;
}
[...]
}
public class CaseWorkerA extends CaseWorker {
[...]
}
Code:
public class ProjectDao {
[...]
public Project saveUpdateProject(final Project project) {
if (project.getId() == null) {
getSession(false).save(project);
} else {
project = (Project) getSession(false).merge(project);
}
getHibernateTemplate().flush();
return project;
}
}
Now, when i have a persisted Project, which is connected to a CaseWorkerA that has a status (both in the database and in the object to be persisted), and send it to saveUpdateProject, the status of the CaseWorkerA becomes null. I really can't see why this is happening, everything seems to be set up correctly..
Link to case on stackoverflow: http://stackoverflow.com/questions/12216138/hibernate-merge-loses-data