-->
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: How to convert byte[] to Blob in Java(Hibernate)
PostPosted: Tue Apr 15, 2008 12:57 am 
Newbie

Joined: Tue Apr 15, 2008 12:44 am
Posts: 1
Hibernate version: 1.3

Name and version of the database you are using: MYSQL 5.0

I am getting the image in the form of a Byte array... and ?I have to write it as Blob through Hibernate in mySQL database....


Plz help me in finding the solution to this...


Thanks in advance....

Hammad


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 8:20 pm 
Newbie

Joined: Fri Apr 04, 2008 2:12 pm
Posts: 11
That is a good thing. You should send your Blobs as a byte[] through Hibernate and into your MySQL DB.

Example for your mapping file:

<property name="ABlob" column="aBlob" type="byte[]"/>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 9:32 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Similarly, you can do it with a JPA annotation. Here's some Hibernate3 code:

Code:
@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);
  }
}


It's just a simple example, but it does show an annotation mapping. You can see more of the tutorial here:

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

_________________
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.