Reputation: 4991
Hello i have a snippet like this.
public void update(Student student)
{
student=super.merge(student);
currentSession().update(student);
return;
}
But sometimes this code throws
java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity.
I was wondering how is this possible merge may return null in any circunstance?
Because i have checked Student
is not null because if is null merge
would throw.
Exception in thread "main" java.lang.IllegalArgumentException: attempt to create merge event with null entity
The only situation i think is happening this stuff is if merge
is returning null is this possible??
Sorry if this question is simple thanks a lot a best regards from Venezuela..
Upvotes: 1
Views: 1880
Reputation: 164
merge() is used to merge a detached object with an attached object (both have the same id). For example, you passed in a student1 object in method update(), which has an id and its status is detached:
public void update(Student student1)
{
//student1 is in detached status, you may modify it if not yet done
student1.setName("ABC");
//now load a student2 from db with the same id, note student2 is in persistent status
Student student2 = currentSession().load(Student.class, student1.getId());
//merge student1 to student2 and return a new student3
Student student3 = currentSession().merge(student1);
//--done, checkout student3, you will see "ABC" is merged. You can't call update() in this case.
return;
}
If you just want to update passed in student, then remove student=super.merge(student);
, call saveOrUpdate(), like this:
public void update(Student student)
{
currentSession().saveOrUpdate(student);
return;
}
Upvotes: 1