-->
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.  [ 4 posts ] 
Author Message
 Post subject: MySQL+Hibernate- how to create/store a blob
PostPosted: Wed Aug 27, 2003 3:10 pm 
Newbie

Joined: Wed Aug 27, 2003 3:03 pm
Posts: 1
Hi,

I wantto store an uploaded image in the DB as a BLOB.
The image data comes as org.apache.struts.upload.FormFile.

In my presistance class I have a property of type
java.sql.Blob mappd to blob in DB.

So, what do I need to do in order to store the image?

I can get byte array from the form file. But which Blob to create in order to pass to my presistance class propery?

MySQL driver has implementation of Blob I don't see any public constructor.

Am I missing something?
Please help.

Thanks in advance,
Igor


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 3:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate.createBlob()


Top
 Profile  
 
 Post subject: problem with Blob
PostPosted: Sat Sep 06, 2003 6:16 am 
Newbie

Joined: Sat Sep 06, 2003 5:59 am
Posts: 1
I've created one Blob object in my persistance object through Hibernate.createBlob(). but after I save this persistance object, there is no data in the MEDIUMBLOB field. Could you help me?


thanks in advance.

bruni


Top
 Profile  
 
 Post subject: example
PostPosted: Sat Sep 06, 2003 9:09 am 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
This works with PostgreSQL and OID field (BLOB)

String fileType = null;
String filename = null;
InputStream data = null;
int FileLength = 0;
java.sql.Blob fileData = null;

if (!theFile.isMissing()) {
fileType = theFile.getContentType();
filename = theFile.getFileName();
FileLength = theFile.getSize();
// create a uniuque name
java.io.File tempfile = java.io.File.createTempFile("" + Math.random(),null);
//tempfile = theAsset.getTempFilePath() + Math.random();
// save the file so that we can create an input stream with it.
theFile.saveAs(tempfile.getAbsolutePath());

fileData = Hibernate.createBlob(new FileInputStream(tempfile),FileLength);
SessionFactory sessions = ds.buildSessionFactory();
Session sess = sessions.openSession();
Transaction tx = sess.beginTransaction();
Ad obj_ad = new Ad();
obj_ad.setFileData(fileData);
obj_ad.setFileType(fileType);
obj_ad.setFileName(filename);
obj_ad.setFileLength(new Long(FileLength));
sess.save(obj_ad);
sess.flush();
tx.commit();
}


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