facebook

[Closed] mysql Blob is String in Hibernate

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #222142 Reply

    zaid
    Member

    Hi all,

    I am a new to the Hibernate technology.

    Before Hibernate, I used to store the uploaded photos in the mysql database, and the code was like this:
    ————————————————-
    PreparedStatement prep=myConnection.prepareStatement(“UPDATE series SET photoBinary=? WHERE seriesID='”+sSeriesID+”‘ “);

    prep.setBytes(1,sloganTitlePhoto.getFileData());
    ———————————————————-
    where sloganTitlePhoto is of type org.apache.struts.upload.FormFile

    Now, in Hibernate, the xml files and the generated class is giving String for the blob in table instead of byte[]. I am trying to store the uploaded the photo but it seems something missing in the conversion between the String and byte array ( the data is stored in database, but the information seems not correct).

    My question is:
    1. Why Myeclipse assumes that the blob in MySQL database is equivalent to String in Java ? shouldn’t be byte[] ?
    2. what’s the solution for my problem ? is there a conversion method from String to Byte[] ? I only need to store a photo binary data in table and fetch it.

    Thank you,
    Zaid

    #222204 Reply

    support-jeff
    Member

    Zaid –

    1. Custom column-to-property type mappings are not supported in MyEclipse yet. This feature is coming in a future release (no timeline at this time). For now, you will have to modify the generated code yourself, bearing in mind that re-generation of the code will overwrite your changes.

    2. I would do some reading in the Hibernate docs on hibernate.org, and look at the wiki contributions for answers to your particular question. From your description, it seems to work, however. What is it that about the data saved that “seems not correct”?

    #222209 Reply

    zaid
    Member

    Dear Jeff,

    what I meant by “seems not correct” is that the photo data is stored in the database, but some characters are not correctly stored, in other words, the photo is invalid in the database.

    I think this problem is due to the fact that the String has some special characters.

    Anyhow, I managed to solve the storing process by doing the following
    ————————————————–
    uploadedPhoto is org.apache.struts.upload.FormFile

    ……
    Photos.setPhotoBinary(String data) ………………
    …..
    sess=SessionFactory.currentSession();
    Photos photo=new Photos();
    photo.setPhotoBinary(new String(uploadedPhoto.getFileData(),”ISO-8859-1″));
    sess.save(photo);
    —————————————————

    Now, the problem is in the data fetching, I haven’t found a way to sort it out, I would be appreciated if you could find a way to let it work.

    Regards,
    Zaid

    #260422 Reply

    danielkohl
    Participant

    Hi,

    have you solved this problem?

    Is there a way to create a String-Object without any conversion?
    I’ve a JPEG converted to an byte[]-Array. Writing this to a file is is OK (the is viewable).
    Writing a “new String(byte[] array)”, because Hibernate expects a String for a BLOB fails in JPEG-viewer due to the String-conversion…

    Greetings

    Daniel

    #260438 Reply

    Haris Peco
    Member

    Daniel,

    Set ‘Hibernate type’ for your blob column to ‘blob’ (3th page of Hiberante mapping wizard) and
    MyEclipse will generate java type Blob (and blob in hbm.xml).
    Now, you can use standard Blob’s getBytes/setBytes/setBinaryStream for manipulate table’s data

    If you want that all your blob column have mapping to hibernate type ‘blob’ (java.sql.Blob in POJO)
    set it in page 2 of wizard in group ‘Customized type Mappings’ next :

    JDBC Type Hibernate type ….
    BLOB blob

    Best
    Peco

    #260451 Reply

    danielkohl
    Participant

    Hmmm….this seems to be a bug for me.

    Fixing it this way will cause some overhaed each time i do reverse engineering me DB.

    I’ve it now this way:

    
     
    public class ImageUtils {
    
        
         public static ByteArrayOutputStream convertToByteArrayOutputStream(String imagepath, int quality) throws IOException {
            BufferedImage image = ImageIO.read( new File(imagepath));
            ByteArrayOutputStream out =new ByteArrayOutputStream();
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
            quality = Math.max(0, Math.min(quality, 100));
            param.setQuality((float) quality / 100.0f, false);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(image);
            out.close(); 
            return out;
         }
    
         public static ByteArrayOutputStream convertToByteArrayOutputStream(String imagepath) throws IOException {
             return convertToByteArrayOutputStream(imagepath, 100);
         }
    
         public static String convertImageFileToString(String imagepath) throws IOException {
             byte arrayBytes[] = convertToByteArrayOutputStream(imagepath, 100).toByteArray(); 
             String str64 =  new BASE64Encoder().encode(arrayBytes);
             return str64;
         }
    
         public static Image convertStringToImage(String string) throws IOException {
             byte arrayBytes[] = new BASE64Decoder().decodeBuffer(string);
             return new ImageIcon(arrayBytes).getImage();
         }
    
    
         
    }
    

    The picture is stored Base64-encoded in the DB. Unfortunately loading the String (from DB) again fails.

    Greetings

    #260459 Reply

    Haris Peco
    Member

    Daniel,

    Have you tried map to blob.If you want map to String then you have to make user type, but it is necessary.
    I agree that it is bug and we will check this.

    Thanks
    Peco

    #260607 Reply

    Brian Fernandes
    Moderator

    Daniel,

    Just wanted to point out that once you make the settings Peco recommended in his previous post, they will be saved and will be in effect for all tables that are reverse engineered to that project in future.

    Sometimes the RE engine will not use the mapping or the type you have in mind (though in this case it should have), the settings on page 2 and 3 allow you to override (permanently for that project) the decision taken by the RE engine.

    Hope this helps,
    Brian.

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: [Closed] mysql Blob is String in Hibernate

You must be logged in to post in the forum log in