开发者

Save null value to many-to-one hibernate relationship error

开发者 https://www.devze.com 2023-04-13 03:52 出处:网络
I have two classes - Person and Department. There is a many-to-one relationship between Person and Department.

I have two classes - Person and Department. There is a many-to-one relationship between Person and 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
}

The program saves the Person object by calling:

  getHibernateTemplate().saveOrUpdate(person);

When the department value of Person object is set before saving, the program works fine. When the department value of Person object is null before saving, I got the following error:

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

Is there any settings missed for the many-to-one relationship? Please advise, thanks.

I have defined the Struts2 + Spring + Hibernate and the person instance is saved by calling

getHibernateTemplate().saveOrUpdate(person);

I have already tried to add

@org.hibernate.annotations.Cascade
   ({org.hibernate.annotations.CascadeType.SAVE_UPDATE,   
          org.hibernate.annotations.CascadeType.DELETE_ORPHAN}) 

but this will automatically created an additional record in Department when saving null to the many-to-one relationship!

For example, I have 3 records MIS, PURCHASING and ADMIN in Department and the person instance is with department saved as开发者_JS百科 MIS originally. When I try to save the person instance with department set as null. There are 4 records MIS, PURCHASING, ADMIN and MIS in Department and the person instance is saved with department set as the new MIS record, not null! Do you have any simple many-to-one relationship sample which is with null value saved to the relationship column? Much thanks.


What you've shown isn't enough to give a definitive answer. Try creating an SSCCE.

With the code you've shown--except that it's missing some important annotations, which I assume you simply omitted when posting--you'd get that exception if you did this:

Session session = ...
Transaction tx = session.beginTransaction();
Person person = new Person();
person.setDepartment(new Department());
session.save(person);
tx.commit();

Since you have no cascading defined on the Person -> Department relationship, you'd have to explicitly save both Person and Department for this to work. Without the save of Department, it's transient, and it's impossible to make only part of an object graph persistent.


May be you are missing @Cascade({CascadeType.SAVE_UPDATE})?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号