-->
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.  [ 3 posts ] 
Author Message
 Post subject: Blob retrieval in Hibernate 3
PostPosted: Wed Mar 16, 2005 4:35 pm 
Beginner
Beginner

Joined: Tue Jan 18, 2005 6:44 pm
Posts: 39
I have the following mappings in my config file and Im using the ojdbc14.jar and the oci driver for the oracle 10g database.

<property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.jdbc.use_streams_for_binary">true</property>

My persistent classes have the following setter and getters

public byte[] getTemplateValue () {
return templateValue;
}

public void setTemplateValue (byte[] templateValue) {
this.templateValue = templateValue;
}


The retrieval returns a larger byte array than what was set using the setter.

In hibernate 3, is there a need to use the BinaryblobType implementation to map the blob objects into a byte[]? The usertype interface has changed since hibernate 2.x so what changes would need to be made to the binaryblobtype implementation?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 1:10 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
Code:
import java.io.Serializable;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

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]);
    if (!rs.wasNull())
      return blob.getBytes(1, (int) blob.length());
    else
      return null;
  }

  public void nullSafeSet(PreparedStatement st, Object value, int index)
      throws HibernateException, SQLException
  {
    if (null == value)
      st.setNull(index, Types.BLOB);
    else
      st.setBlob(index, Hibernate.createBlob((byte[]) value));
  }

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

    byte[] bytes = (byte[]) value;
    byte[] result = new byte[bytes.length];
    System.arraycopy(bytes, 0, result, 0, bytes.length);

    return result;
  }

  public boolean isMutable()
  {
    return true;
  }

  public int hashCode(Object x) throws HibernateException
  {
    return 0;
  }

  public Serializable disassemble(Object value) throws HibernateException
  {
    return (Serializable)value;
  }

  public Object assemble(Serializable cached, Object owner) throws HibernateException
  {
    return cached;
  }

  public Object replace(Object original, Object target, Object owner) throws HibernateException
  {
    return original;
  }
}

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject: does that work for you?
PostPosted: Tue Apr 26, 2005 7:54 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
I tried that code with Hibernate 3.0.1 and Oracle 10g, but it doesn't work:

Code:
java.lang.ClassCastException
        at oracle.jdbc.driver.OraclePreparedStatement.setBlob(OraclePreparedStatement.java:5730)
        at weblogic.jdbc.wrapper.PreparedStatement.setBlob(PreparedStatement.java:335)
        at util.BinaryBlobType.nullSafeSet(BinaryBlobType.java:50)
        at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:140)
        at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1626)
        at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1603)
        at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1859)
        at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2209)
        at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
...


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