-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to deal with JBoss MarshalledValue serialized columns
PostPosted: Mon Nov 15, 2004 1:19 am 
Newbie

Joined: Fri Nov 12, 2004 4:33 am
Posts: 7
Location: Austin, Texas
I'm using Hibernate to access data that was created with JBoss CMP. I have some big String fields that are stored as Objects and thus seralized into a BLOB (in particular, a MySQL longblob).

I seem to remember that JBoss actually stores these serialized values as org.jboss.invocation.MarshalledValue types (for example, see http://www.jboss.org/index.html?module=bb&op=viewtopic&t=22588), making them difficult to read without JBoss. Aside from writing a conversion program (which is all I can currently think of), is there any fairly straightforward way to access this serialized column using Hibernate? Thanks

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 8:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Is there some issue with writing a UserType that uses the JBoss class for ser/deser purposes? By far the easiest and most straight-forward.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 6:03 pm 
Newbie

Joined: Fri Nov 12, 2004 4:33 am
Posts: 7
Location: Austin, Texas
Thanks Steve, I think that will work perfectly. I'm a week into Hibernate and loving it..

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 2:09 am 
Newbie

Joined: Fri Nov 12, 2004 4:33 am
Posts: 7
Location: Austin, Texas
I thought I'd share the custom UserType I wrote to allow Hibernate to deal with JBoss' MarshalledValues that it stores for serialized objects in a BLOB column. Thanks to Steve for the suggestion. I have tested this, but it's my first custom UserType so beware.

Code:
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;
import net.sf.hibernate.lob.BlobImpl;
import net.sf.hibernate.util.SerializationHelper;
import org.jboss.invocation.MarshalledValue;

/**
* Provides Hibernate access to BLOB Objects serialized by JBoss 3.x
* CMP, which JBoss wraps in a
* org.jboss.invocation.MarshalledValue wrapper.
*
*/

public class MarshalledValueUserType implements UserType {
   private static final int[] SQL_TYPES = { Types.BLOB };

   public MarshalledValueUserType() { }
   public int[] sqlTypes() { return SQL_TYPES; }
   public Class returnedClass() { return String.class; }
   public boolean equals(Object x, Object y) { return x == y; }
   public Object deepCopy(Object value) { return value; }
   public boolean isMutable() { return false; }

   public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
      Object o = rs.getObject(names[0]);

      if (o instanceof MarshalledValue) {
         MarshalledValue mv = (MarshalledValue) o;
         try {
            return mv.get();
         } catch (Exception e) {
            throw new HibernateException("Exception unwrapping MarshalledValue", e);
         }
      } else {
         if (o != null) {
            System.out.println("Warning: I expected a MarshalledValue object, but got: "+o.getClass().getName());
         }
         return null;
      }
   }

   public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
      try {
         // Wrap the Object with a JBoss MarshalledValue
         MarshalledValue mv = new MarshalledValue(value);
         byte[] array = SerializationHelper.serialize(mv);
         st.setBlob(index, new BlobImpl(array));
      } catch (IOException e) {
         throw new HibernateException("Exception wrapping object in MarshalledValue", e);
      }
   }
}


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