-->
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.  [ 1 post ] 
Author Message
 Post subject: How to get object's type in UserType nullSafeGet
PostPosted: Tue Apr 10, 2012 12:51 am 
Newbie

Joined: Mon Sep 22, 2008 10:34 am
Posts: 5
I am writing the following custom serializable UserType:

Code:
public class SerUserType extends MutableUserType {
    protected Class klass;
    protected SerA ser=F.g(SerA.class);
    public Class returnedClass() {
        return klass;
    }
    public int[] sqlTypes() {
        return new int[] {Types.BLOB};
    }
    public boolean equals(Object x, Object y) throws HibernateException {
        return ObjectUtils.equals(x, y);
    }
    public Object deepCopy(Object value) {
        klass=value.getClass();
        Copyable copyable=(Copyable)value;
        Object copy=copyable.copy();
        return copy;
    }
    public Object nullSafeGet(ResultSet resultSet,String[] names,SessionImplementor session,Object owner)
        throws HibernateException,SQLException {
        byte[] b=(byte[])BlobType.INSTANCE.nullSafeGet(resultSet,names,session,owner);
        return ser.deser(b,klass);
    }
    public void nullSafeSet(PreparedStatement preparedStatement,Object value,int index,SessionImplementor session)
        throws HibernateException,SQLException {
        BlobType.INSTANCE.nullSafeSet(preparedStatement,ser.ser(value),index,session);
    }
}


In case you're interested, I might even override the registered serialable type with it. The implementation of SerA is the protostuff serializer.

Anyway, it doesn't appear that any of the methods providing the object in this interface are called before nullSafeGet so it is impossible to determine the class of the object we're working with, making it impossible to obtain the class for the serialization call.

Therefore, it appears that the only solution is to make a ParameterizedType and pass the object's class as a property. :(

I think the API should be changed to pass the object class in nullSafeGet

Please post if you have any other ideas.


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

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.