-->
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.  [ 5 posts ] 
Author Message
 Post subject: how hibernate stores object in a blob
PostPosted: Wed Dec 22, 2010 4:18 am 
Newbie

Joined: Wed Dec 22, 2010 2:32 am
Posts: 3
In my case Object is getting saved in a blob format but while we try to read and save that blob in a file it is not getting read giving EOFException and available() method of ObjectInputStream shows 0 that means there is nothing to be read . Then I checked in DB there was data in a blob format.
Can anyone help in in understanding that how hibernate stores an object in a blob format. What I analyzed is it is adding some special character while saving object in DB so that not able to convert byte to object format, as it is giving EOFException . The code I am writting is

ByteArrayInputStream bais;
ObjectInputStream in;
try {
bais = new ByteArrayInputStream(rcmCommunicationBean1);
in = new ObjectInputStream(bais);
ObjectStreamClass myObject = ObjectStreamClass.lookup(
Class.forName("com.successfactors.rcmcommon.bean.email.RCMCommunicationBean"));
resolveClass(myObject);
Object obj = new Object();
while ((obj = in.readObject()) != null) {
if (obj instanceof RCMCommunicationBean) {
rcmCommunicationBean = (RCMCommunicationBean)obj;
}
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}


Top
 Profile  
 
 Post subject: Re: how hibernate stores object in a blob
PostPosted: Wed Dec 22, 2010 5:00 am 
Regular
Regular

Joined: Fri Nov 12, 2010 4:13 am
Posts: 81
Location: India
You want to read already saved blob data from database and want to display it right?

_________________
Thanks & Regards,
Chirag


Top
 Profile  
 
 Post subject: Re: how hibernate stores object in a blob
PostPosted: Wed Dec 22, 2010 5:10 am 
Newbie

Joined: Wed Dec 22, 2010 2:32 am
Posts: 3
Yes but the case is that in our project we have change the package structure. So while reading the blob we need to modify the class path stored in blob so we are first reading it as byte and then trying to modify it and deserialize the blob to the get the object. But while fetching the object from the blob it is giving EOFException


Top
 Profile  
 
 Post subject: Re: how hibernate stores object in a blob
PostPosted: Wed Dec 22, 2010 5:29 am 
Regular
Regular

Joined: Fri Nov 12, 2010 4:13 am
Posts: 81
Location: India
I have one class written from my project which reads blob data from database and display the data stored.I m copying my code here..It may help you somehow i guess.


Code:
public static void getImage(HttpServletResponse response,String site_sketch_id) throws Exception {
         java.sql.Connection conn = null;
         String query = null;
         Statement stmt = null;
         ResultSet rs = null;
         conn = LookUpUtil.getConnection(true, true);
         traceLogger.info("site_sketch_id : " + site_sketch_id);
         response.setContentType("text/html;charset=UTF-8");
         ByteArrayOutputStream baos = new ByteArrayOutputStream();

         query = "select site_sketch from sketch_info where pk_book_id ='" + site_sketch_id + "' ";
         traceLogger.info("query : " + query);
         
         try {
             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
     
             response.setHeader("Content-Disposition","attachment;filename=SiteSketchImage.jpg");//for save option
             response.setContentType("application/image");
               
             rs = stmt.executeQuery(query);
             while(rs.next()){   
                 byte[] imgData=rs.getBlob("site_sketch").getBytes(1,(int)rs.getBlob("site_sketch").length());
                 baos.write(imgData);
             }
               
            // the contentlength is needed for MSIE!!!
            response.setContentLength(baos.size());
            // write ByteArrayOutputStream to the ServletOutputStream
            ServletOutputStream out = response.getOutputStream();
            baos.writeTo(out);
            out.flush();
            out.close();
             
         } catch (Exception e) {
             traceLogger.info("Exception " + e);
         } finally {
             if (stmt != null)
                 stmt.close();
             if (rs != null)
                 rs.close();
             if (conn != null) {
                 conn.close();
                 conn = null;
             }
         } //End Finally
     } //End Of getImage

_________________
Thanks & Regards,
Chirag


Top
 Profile  
 
 Post subject: Re: how hibernate stores object in a blob
PostPosted: Wed Dec 22, 2010 7:17 am 
Newbie

Joined: Wed Dec 22, 2010 2:32 am
Posts: 3
No that is not what i am looking for.
Can anyone tell me how hibernate internally stores object as a blob so that in same manner I can also retrieve the object by reading data as a blob.


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