-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to save image to database using Hibernate?
PostPosted: Sun Jul 11, 2004 6:49 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Hi everyone:

Had anyone saved picture to database using Hibernate successfully?
Can hibernate proccess Blob data type ? I have a table that has a Blob field.(I am using MySQL). It can't save the image if I use the code :

Code:
PrintWriter pw=null;
      resp.setContentType("text/html;charset=gb2312");
         InputStream in=req.getInputStream();
         byte b[]=new byte[in.available()];
         int i=-1;
         while((i=in.read())!=-1){
            in.read(b);
            System.out.println("Get byte[]: "+i);
         }
         
try{
Session _session=HibernateUtil.currentSession();
         Transaction t=_session.beginTransaction();
         Buildingpicture bp=new Buildingpicture();
         bp.setPicture_memo("qijiashe upload");
         bp.setPicture_name("Lyopic");
   
         System.out.println("Get Picture : "+b.toString());
         bp.setPicture_file(Hibernate.createBlob(in));
                         
          _session.save(bp);
         
          _session.flush();
            t.commit();

          HibernateUtil.closeSession();
          in.close();
          pw=resp.getWriter();
          pw.println("Sucess...");



It is a Servlet. I want to save a user's upload picture to database. But the picture can't be saved to MySQL. I can get the inputStream indeed.
Is there any error in my code?
Thks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 12, 2004 9:36 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
I'm not quit sure but I think you must close the stream before saving it or using the size of the array (i) as second parameter.

regards

Olaf


Top
 Profile  
 
 Post subject: No
PostPosted: Mon Jul 12, 2004 9:40 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
I close the Stream after I save the Object.So I think it is not the problem.
Anyone save image to database successful? Help!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 7:59 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
I save a lot of snapshot to a postgres database, but instead of blobs I use byte arrays. Saving and reading works fine.

I think you have to close the stream before saving it. How else the EOF should be reached without setting the number of bytes to be stored?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 11:05 am 
Newbie

Joined: Fri Aug 06, 2004 8:10 am
Posts: 9
Location: Brazil
Kaneda wrote:
I save a lot of snapshot to a postgres database, but instead of blobs I use byte arrays. Saving and reading works fine.

I think you have to close the stream before saving it. How else the EOF should be reached without setting the number of bytes to be stored?


Kaneda, can you give me a example or how to read a Blob.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 11:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/73.html may help

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 11:54 am 
Beginner
Beginner

Joined: Mon Aug 16, 2004 6:09 am
Posts: 46
Location: Geneva, Switzerland
Quote:
<property name="picture" type="binary">
<column name="picture" sql-type="blob"/>
</property>


works fine (in oracle, at least) providing the following property set

hibernate.jdbc.use_streams_for_binary = true
hibernate.jdbc.batch_size = 0

It maps blob sql type to byte[] java type.


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