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.  [ 5 posts ] 
Author Message
 Post subject: Interceptor problem: previousState == null in onFlushDirty()
PostPosted: Thu Mar 27, 2008 4:19 pm 
Newbie

Joined: Thu Mar 27, 2008 4:13 pm
Posts: 5
Hibernate version:

3.2.6.ga

Code between sessionFactory.openSession() and session.close():

Code:
@Override
    public boolean onFlushDirty(Object entity, Serializable id,
            Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
        if (entity instanceof Historizable) {
            Set<HistoryEntry> historyEntries = new HashSet<HistoryEntry>();

            for (int i = 0; i < currentState.length; i++) {
                HistoryEntry entry = new HistoryEntry();
                assert BaselineClientServiceImpl.currentUser != null;
                entry.setUser(BaselineClientServiceImpl.currentUser);
                entry.setAction(UPDATE);
                entry.setEntityName(entity.getClass().getSimpleName());
                entry.setPropertyName(propertyNames[i]);
                // NPE here due to previousState being null
                entry.setOldValue(previousState[i] == null
                                      ? null
                                      : previousState[i].toString());
                entry.setNewValue(currentState[i] == null
                                      ? null
                                      : currentState[i].toString());
               
                historyEntries.add(entry);
            }
            saveHistoryEntries(historyEntries);
        }
        return false;
    }


Full stack trace of any exception that occurs:

NPE on the line accessing previousState above

Name and version of the database you are using:

MSSQL Server 9.0

Why would previousState ever come in as a null reference? I understand that the previousState array may itself contain nulls, but the array shouldn't be null.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 4:14 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
i am not sure but could be that u call session.update() on previusly detached object so the session doesn`t know previous state.

if it is so, try to use session.merge() instead of update()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 11:08 am 
Newbie

Joined: Thu Mar 27, 2008 4:13 pm
Posts: 5
Thank you! I was, in fact, using update() on a newly created object that was detached, so merge() did the trick.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 12:23 pm 
Newbie

Joined: Thu Mar 27, 2008 4:13 pm
Posts: 5
Actually, coming back to this: Due to the infinite loop problem I'm having in connection with calling merge(), I'm trying to use update(). Since it might be a problem with the object being detached, I am ensuring that I call save() on the object after I load it from the database, which shouldn't be necessary, right? I'm loading it using the following code:

Code:
public <E extends AbstractPersistentEntity<?>> E getByMultipleFieldValues(
         Class<E> returnType,
         String[] fieldNames,
         Object[] values) {
      Session session = getCurrentSession();
      try {
         Criteria cri = session.createCriteria(returnType);
         for (int i=0; i<fieldNames.length; i++) {
            if (values[i] == null) cri.add(Restrictions.isNull(fieldNames[i]));
            else cri.add(Restrictions.eq(fieldNames[i], values[i]));
         }
         E ape = (E) cri.uniqueResult();
         if (ape != null)
            ape.setPersisted(true);
         return ape;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }


The returned object ought to be attached to the session, yes? I then update it with new values and call this code:

Code:
public <E extends AbstractPersistentEntity<?>> void saveAll(Set<E> set, boolean alreadyPersisted) {
       Session session = getCurrentSession();
       Transaction t = session.beginTransaction();
      for (AbstractPersistentEntity<?> pe : set) {
         try {
            if (alreadyPersisted)
               session.update(pe);
            else
               session.save(pe);
         } catch (NonUniqueObjectException nuoe) {
            System.out.println("NUOE: " + nuoe.getIdentifier());
            session.merge(pe);
         }
      }
      try {
         t.commit();
      } catch (Exception e) {
         t.rollback();
         e.printStackTrace();
      }
   }


I walked through and made sure that "alreadyPersisted" was true, thus session.update() was getting called. However, as I was walking through the Hibernate code, it seemed to thing that the object was not attached to the session, so the history was lost, and thus when my HistoryInterceptor's onFlushDirty() method was called, the previousState passed to it was null. At the very least now, I have some more insight into the working of the code, but not much more insight into the nature of the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 7:21 am 
Newbie

Joined: Mon Sep 08, 2008 7:13 am
Posts: 1
Still struggling with same problem, i m using spring hibernate template and getting previous state as null in HibernateInterceptor.

Does the object become detached immediately after we retrieve the object using spring hibernate template load/get method.

Is there a way to get previous state with out using merge instead of update?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.