-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate 3.0.5 problem with interceptor and not-null column
PostPosted: Thu Dec 15, 2005 6:10 am 
Newbie

Joined: Thu Dec 15, 2005 3:53 am
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 1:41 pm 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Hi Janne,

I made alot with Interceptor during my last university work where I implemented an historization module base don hibernate.

Did not try you code sofar, but what happens when you replace it by 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());
session.refresh(announcement);



guess you do a commit() somewhere below..

Good luck

Marcus


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.