Menu

Being lazy with blobs

Help
2006-02-10
2013-04-25
  • Akram BEN AISSI

    Akram BEN AISSI - 2006-02-10

    Hello all,

    My webapp is getting data from the business layer application using EJBs.

    Persistence is made with Hibernate in the business layer. So getting the Blob locator in most cases is good form me.
    e.g: List findFiles();

    But sometimes, I need to get the entire data. like FileModel findFile(id);

    I tried to let the hibernate property on file.getContent() and then I do
    file.setBlobContent(Hibernate.createBlob(file.getContent()));

    but this doesn't work. My content still returns 86 bytes.

     
    • Joe D. Velopar

      Joe D. Velopar - 2006-02-10

      Can I assume both your business layer and presentation layer are run in the same JVM ?  Otherwise, the only way to lazy access the blob is to have a separate request to the business layer.

      Someone suggested (at http://hansonchar.blogspot.com/2005/06/oracle-blob-mapped-to-byte-in.html\) the use of hibernate.jdbc.use_streams_for_binary=true in hibernate.properties, have you tried that to see if it works ?  (Admittedly I haven't tried it myself.)

      Another hack is to map two different classes to the same underlying entity, one without the blob and one with the blob as the only attribute.  Lazy loading is then achieved by accessing the one with the blob.

       
    • Akram BEN AISSI

      Akram BEN AISSI - 2006-02-10

      Indeed, both layers does not run in the same JVM. So I have made two different queries to deal with this problem.

      I didn't try the hibernate.jdbc.use_streams_for_binary=true option. I will take a look right now.

      Regarding the hack, I also had the idea and set it up in 2 differents manner.

      First I wrote 2 classes.
      FileBaseModel containing name and size attribute
      FileModel extendinf FileBaseModel containing a byte[] content. Getting/setting the content is done by the set/getContentBlob methods (I put a logger.debug in this method to show calls).

      Then I created two mappings for those 2 different classes (using xdoclet).
      The generated mappings contain:
      size and name column for FileBaseModel
      size, column and content for FileModel

      Unfortunately, when I do a:
      getHibernateTemplate().find(" from FileBaseModel " ).... I can see that the getBlobContent is called and that the instanciated class is an FileModel object.
      Do you have an explanation about this strange behaviour ?

      Then, When I want to retrieve the blobbed file I do a find( " from FileModel where id = ?", criteria);

      The second way is to set Hibernate polymophism (with subclass) but this requires a discriminator colum, and this is not acceptable and not workable.

       
      • Joe D. Velopar

        Joe D. Velopar - 2006-02-10

        "from FileBaseModel" means retrieving all instances of FileBaseModel, which would include instances of FileModel!  That's not the behavior you want.

        Consider creating a common abstract base class, say AbstractFileModel, which both FileBaseModel and FileModel ** directly ** extend.

        Now "from FileBaseModel" and "from FileModel" should work as expected.  For your purposes, you never need to do a "from AbstractFileModel".

         
    • Akram BEN AISSI

      Akram BEN AISSI - 2006-02-10

      Well, for me, it is week-end time, I will try this as soon as arrive on monday, and tell you if this work. But it seems to be a good idea.

      By the way, I tried the hibernate.jdbc.use_streams_for_binary=true
      This did nothing for me.

      Have a nice day, thank you for your help.

      Akram

       
    • Akram BEN AISSI

      Akram BEN AISSI - 2006-02-13

      Well done !

      It works great, thank you for your help.

       
      • karai

        karai - 2006-03-01

        Hi,

        i  am facing some problem in retriving Blob filed from mssql database.

        if you can help me please mail me karai_s@yahoo.co.in

         

Log in to post a comment.