HIBERNATE JBoss.org
 |  Register  | 
     
News 
About 
   Feature List 
   Road Map 
Documentation 
   Related Projects 
   External Documentation 
Download 
Forum & Mailinglists 
Support & Training 
JIRA Issue Tracking
Wiki Community Area


Hibernate Public Training Courses


Get Hibernate in Action eBook!


JavaWorld 2003 Finalist


Jolt Award 2004 Winner
      
Documentation > Community Area > UserType for audit info logging > Comments > A few modifications


Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
Subject: A few modifications 03 Nov 2003, 11:13
From: tmckinnon Replies: 1, Views: 608
 
I have been playing around with the AuditInterceptor and I noticed 
that it didn't work if you had 'dynamic-update' set.
 
It didn't recognise the modifications you do in the Interceptor 
because it has already determined the optimized update statement.

Note: also to support the newest Interceptor Interface you need all of 
the following methods implemented. These were not in the example. 
(from what I could see)
 
To fix this I did something like this:
 
 public Boolean isUnsaved(Object o) {
        return null;
    }
 
    public int[] findDirty(Object o, Serializable serializable, Object
[] objects, Object[] objects1, String[] strings, Type[] types) {
        ArrayList ints = new ArrayList();
        if(objects == null || objects1 == null){
            return null;
        }
        for(int i = 0; i < objects.length; i++) {
            Type type = types[i];
            if(type.getReturnedClass().equals(AuditInfo.class)) 
continue;
            try{
                Object current = objects[i];
                Object previous = objects1[i];
                System.out.println("strings[i] = " + strings[i]);
                System.out.println("previous = " + previous);
                System.out.println("current = " + current);
                if(current == previous) continue;
                if(current != null && previous != null){
                    if(previous.toString().equals(current.toString())){
                       continue;
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.println("adding i = " + i);
            ints.add(new Integer(i));
        }
        if(ints.size() > 0){
            int[] intArray = new int[ints.size()+1];
            int ind = 0;
            for(Iterator iterator = ints.iterator(); iterator.hasNext
();) {
                Integer integer = (Integer) iterator.next();
                intArray[ind++] = integer.intValue();
                System.out.println("integer = " + integer);
            }
            intArray[ind++] = strings.length-1;
//            intArray[ind++] = strings.length - 2;
            return intArray;
        }
        return new int[0];
    }
 
    public Object instantiate(Class aClass, Serializable serializable) 
throws CallbackException {
        return null;
    }

 
Which allowed me to only execute updates on the specific fields that 
needed it.  It does however update ALL of the AuditInfo fields; I am 
sure there is a way around this.. but I haven't worked on it yet.
 
You can also modify the findDirty logic to use the Type array to do 
better value checking, but the to string implementation worked for my 
simple needs.
Note: if you have composite types etc you will have to modify this to 
use a compareTo or something.
 
 
Cheers
 
Troy
 

Thread:
 A few modifications 
 tmckinnon   03 Nov 2003, 11:13 
 Re: A few modifications 
 dimas   10 Nov 2003, 21:13 
      

coWiki