Hi,
I'm having trouble with Hibernate interceptor when having not-null="true" in the columns that I set in the interceptor's onFlushDirty or onSave methods.
My hibernate is a version
3.0.5
What I get is the following error:
not-null property references a null or transient value: fi.foobar.model.Announcement.editor
Here's some of the stacktrace:
Stacktrace:
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: fi.vr.foobar.model.Announcement.editor
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:164)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at
...
I have the following properties for the Announcement class:
<property name="creator" not-null="true" type="string" />
<property name="editor" not-null="true" type="string" />
And this is the code I'm using to update the editor (and creator):
Code:
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) throws CallbackException {
boolean updated = false;
for (int i = 0; i < propertyNames.length; i++) {
String propertyName = propertyNames[i];
if ("editor".equals(propertyName)) {
String editor = CurrentSubjectProvider.getCurrentSubject();
state[index] = editor;
return true;
}
}
return updated;
}
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) throws CallbackException {
boolean updated = false;
for (int i = 0; i < propertyNames.length; i++) {
String propertyName = propertyNames[i];
if ("creator".equals(propertyName)) {
String creator = CurrentSubjectProvider.getCurrentSubject();
state[index] = creator;
updated = true;
} else if ("editor".equals(propertyName)) {
String editor = CurrentSubjectProvider.getCurrentSubject();
state[index] = editor;
updated = true;
}
}
return updated;
}
This functionality works fine if I simply save the Announcement object. Both the creator and editor properties are updated correctly even though I don't set them in the service code. The interceptor is the only one that set's their values.
However, this fails if I first save the Announcement object in my code and then in the same method update some of its properties. Like this:
Code:
...
Announcement announcement = new Announcement();
// setting all necessary properties except the creator and editor
...
session.save(announcement);
// This is what makes the problem occur:
annoucement.setUpdatedTime(new Date());
So. Is it possible that Hibernate first checks that all not-null properties are really not null (checkNullability(...)) and only after a valid not null checking it proceeds with my own interceptor?
I would appreciate it a alot if any of you readers have encountered something similar and have some tips for correcting the problem!
Thanks,
Janne