-->
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: Mapping a Blob to a byte[] in hibernate 3
PostPosted: Sun Nov 05, 2006 12:40 pm 
Newbie

Joined: Sun Oct 15, 2006 1:52 am
Posts: 5
Hi,

This article describes how to map a blob to a byte[] in hibernate 1.3.2.
http://www.hibernate.org/73.html

But I would like to know how to do the same in hibernate 3.

If I just copy the same code in Hibernate 3, it won't compile.
In Hibernate3, the UserType interface has more methods,
e.g. disassembles, assemble() and replace().

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 19, 2008 8:43 pm 
Newbie

Joined: Fri Apr 04, 2008 2:12 pm
Posts: 11
To helps others in the future, here is an implementation for Hibernate 3:

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Blob;
import java.io.Serializable;

import org.hibernate.usertype.*;
import org.hibernate.*;

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

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

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

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

public Serializable disassemble(Object value)
{
byte[] bytes = (byte[]) value;
return(bytes);
}

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

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[] bytes = (byte[]) value;
byte[] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);

return result;
}

public boolean isMutable()
{
return true;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 11:01 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
With JPA annotations, it's pretty easy. Here's a tutorial on the topic of column mappings:


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=06hibernatetableandcolumnmappingwithjpa


Code:
package com.examscam.hib.exam;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
@Table(name = "BlobClobBoBob", schema = "examscam")
public class BlobClobber {
  Long id;
  String beans;
  char[] ming;
  Character[] witness;
  java.sql.Clob sqlClob;

  java.sql.Blob sqlBlob;
  byte[] me;
 
  @Id
  @GeneratedValue
  @Column(name = "id")
  public Long getId() {return id;}
  @Lob
  public String getBeans() {return beans;}
  @Lob
  public byte[] getMe() {return me;}
  @Lob
  public char[] getMing() {return ming;}
  @Lob
  public java.sql.Blob getSqlBlob() {return sqlBlob;}
  @Lob
  public java.sql.Clob getSqlClob() {return sqlClob;}
  @Lob
  public Character[] getWitness() {return witness;}

  public static void main(String args[]) {
   AnnotationConfiguration config =
                          new   AnnotationConfiguration();
   config.addAnnotatedClass(BlobClobber.class).configure();
   new SchemaExport(config).create(true, true);
  }
}
      
Image

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.