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 read/write Blob images using Hibernate
PostPosted: Tue Oct 28, 2008 3:49 am 
Beginner
Beginner

Joined: Mon Aug 13, 2007 2:37 am
Posts: 22
Hi guys,

I have a web application where I'm trying to store and retrieve user JPEG images in a MySQL database. I'm using Spring and Hibernate3, but I don't know how to handle blob or binary datatypes.

I would I appreciate it if someone that show my how to configure my hibernate.cfg.xml and how to convert the byte data I get from HTML to the blog datatype I'll be storing in the db using hibernate.

Here's what I have so far:
Code:
<hibernate-mapping>   

<class name="com.RXCoreApp.datamodel.UserImagesCbo" table="User_Images" catalog="RX">
       
<id name="userImageId" type="java.lang.Integer">           
<column name="User_Image_Id" />           
<generator class="identity"></generator>       
</id>       

<many-to-one name="users" class="com.RXCoreApp.datamodel.UserCbo" fetch="select">            <column name="User_Id" not-null="true" />       
</many-to-one>       

<property name="userImagePath" type="java.lang.String">            <column name="User_Image_Path" not-null="true" />        </property>       

<property name="userDefaultFlag" type="java.lang.String">            <column name="User_Default_flag" length="1" />       
</property>       

<property name="imageContent" type="java.sql.blob">           
<column name="Image_Content" sql-type="BLOB" />        </property>     

</class>
</hibernate-mapping>


I'm clueless dealing with blobs...Please help!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 7:19 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
The mapping seems correct (just change java.sql.blob for java.sql.Blob, or just by blob).

To create the blob when you wish to write, just use the helpers that come with the class org.hibernate.Hibernate. There are several static methods called createBlob

You may create blobs form InputStream, byte[] ....

To read data from a blob, just use the method getBinaryStream().


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 4:53 pm 
Beginner
Beginner

Joined: Mon Aug 13, 2007 2:37 am
Posts: 22
I tried your suggestion, BUT the actual BLOB content is not being SAVED in the database. however, the other fields in my USER_IMAGE table are being writed to the database.

Here's what I'm doing:

1. Populate entity bean

Code:
   FormFile imageFile = (FormFile) images[x];    
                       userImageCbo.setUsers(userCbo);
                                 userImageCbo.setUserDefaultFlag(defaultFlag);
                                 userImageCbo.setUserImagePath(destinationPath);
                                 userImageCbo.setImageDataContents( Hibernate.createBlob(imageFile.getFileData() )    );



2. Invoke my Hibernate DAO

Code:

userImagesDao.save(userImageCbo);




3. Spring-Hibernate DAO

Code:
    public void save(UserImagesCbo userImagesCbo) {


        try {
           this.getHibernateTemplate().save(userImagesCbo);

        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }


The database is not throwing an error, but this is the contents of my blob field in MySQL:

Code:

[BLOB - NULL]





what I'm I doing wrong??? Please help


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.