我有两个类 - Person
和Department
。 Person
和Department
之间有多对一的关系。将空值保存为多对一的休眠关系错误
public class Person {
private Integer id;
private String name;
private Department department;
@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=Department.class)
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumn(name="department", referencedColumnName="id", nullable=true, insertable=false, updatable=true)
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
// other getter and setter
}
public class Department {
private Integer id;
private String name;
// other getter and setter
}
程序通过调用保存Person
对象:
getHibernateTemplate().saveOrUpdate(person);
当Person
对象的部门值保存之前设定,程序工作正常。当保存前Person
对象的部门值为空,我得到了以下错误:
object references an unsaved transient instance - save the transient instance before flushing: webmodule.model.Department; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: webmodule.model.Department
是否有任何设置错过了很多一对一的关系?请指教,谢谢。
我已经定义了Struts2的+ Spring + Hibernate的,而该人实例是通过调用
getHibernateTemplate().saveOrUpdate(person);
救我已经尝试添加
@org.hibernate.annotations.Cascade
({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
,但这会自动创建一个额外的记录部门将null保存为多对一关系时!
例如,我有3个记录MIS,PURCHASING和ADMIN在系,而个人实例是以系保存为MIS的。当我尝试将部门集的人员实例保存为空时。在部门中有4个记录MIS,PURCHASING,ADMIN和MIS,并且人员实例与部门集合保存为新的MIS记录,不为空! 您是否有任何简单的多对一关系样本与null
值保存到关系列?非常感谢。