-->
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: Mapping a Blob to a byte[] in hibernate 3
PostPosted: Mon Nov 06, 2006 1:01 pm 
Newbie

Joined: Sun Oct 15, 2006 11:20 pm
Posts: 13
Hi,

I am trying to map map a Blob to a byte[] in hibernate 3.
So I following this example http://www.hibernate.org/73.html (based on hibernate 1.3.2), and create my BinaryBlob Type.

But when I run it , i get this exception, can you please tell me what did i do wrong?

Code:
java.lang.UnsupportedOperationException: Blob may not be manipulated from creating session
   at org.hibernate.lob.BlobImpl.excep(BlobImpl.java:104)
   at org.hibernate.lob.BlobImpl.getBytes(BlobImpl.java:50)
   at org.hibernate.lob.SerializableBlob.getBytes(SerializableBlob.java:35)
   at com.testproject.model.BinaryBlobType.deepCopy(BinaryBlobType.java:60)
   at org.hibernate.type.CustomType.deepCopy(CustomType.java:179)
   at org.hibernate.type.TypeFactory.deepCopy(TypeFactory.java:329)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:274)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)


Here is my code:
Code:

public class BinaryBlobType implements UserType
{
  public int[] sqlTypes()
  {
    return new int[] { Types.BLOB };
  }

  public Class returnedClass()
  {
    return byte[].class;
  }

  public boolean equals(Object x, Object y)
  {
    return (x == y)
      || (x != null
        && y != null
        && java.util.Arrays.equals((byte[]) x, (byte[]) y));
  }

  public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
  throws HibernateException, SQLException
  {
    Blob blob = rs.getBlob(names[0]);
    return blob.getBytes(1, (int) blob.length());
  }

  public void nullSafeSet(PreparedStatement st, Object value, int index)
  throws HibernateException, SQLException
  {
    st.setBlob(index, Hibernate.createBlob((byte[]) value));
  }

  public Object deepCopy(Object value)
  {
    if (value == null) return null;

    byte[] result;
   try {
      SerializableBlob blog = (SerializableBlob) value;
      long l = blog.length();
      
      byte[] bytes = blog.getBytes(0, (int)l);
      result = new byte[bytes.length];
      System.arraycopy(bytes, 0, result, 0, bytes.length);
   } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
      
   }

    return result;
  }

  public boolean isMutable()
  {
    return true;
  }

public Object assemble(Serializable component, Object arg1) throws HibernateException {
   // TODO Auto-generated method stub
   return component;
}

public Serializable disassemble(Object value) throws HibernateException {
   // TODO Auto-generated method stub
   return (Serializable) value;
}

public int hashCode(Object arg0) throws HibernateException {
   // TODO Auto-generated method stub
   return arg0.hashCode();
}

public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
   // TODO Auto-generated method stub
   return arg0;
}

}



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.