Menu

Hibernate Blob

Help
uyu
2006-08-16
2013-04-25
  • uyu

    uyu - 2006-08-16

    I'm pretty new to both Hibernate and Blob's, so this question of mine might seem strange.

    What I'm planning to do is the following:
    - create a pojo that has a byte[] field;
    - create a corresponding hbm that has that field as a blobAsByteArray type;
    - introduce a local class BlobAsByteArray that implements hibernate's UserType;
    - have that class either use net.sf.beanlib.utils.BlobUtils's methods or Hibernate3BeanTransformer (not sure here).

    My question: Am I on the right track here or am I overlooking something.

    Thanx!

     
    • Joe D. Velopar

      Joe D. Velopar - 2006-08-16

      Have you looked at:

      http://hansonchar.blogspot.com/2005/06/oracle-blob-mapped-to-byte-in.html

      and the associated comments ?

      I haven't tried UserType. If you just need a byte[] member field, you can map it with the "blob" type as suggested in the blog.

       
    • uyu

      uyu - 2006-08-17

      Yeah, that's were I got the link to beanlib from. Actually my search started at:
      http://www.hibernate.org/73.html

      However, I'd like to have my pojo's really clean so I can basically create them on the spot from the hbm. I don't have enough background so that I can say "that's what I want" or, "you shouldn't want that". So I want to keep my options open. Besides, it's very possible that I have more blob's in future projects.

      So I figured that implementing usertype could lead to a re-usable and cleaner solution, and have a go at that.

      Also, I read in "Hibernate In Action" that the authors had found the database-drivers behave very differently. Hibernate knows all, which is really great! When I started looking for a Blob solution I first found http://www.hibernate.org/73.html, which basically states that this isn't as easy as it should be, and got me on alert. I'm not so eager of having to do a lot of re-work when the drivers are upgraded. I'm hoping that bean-lib solves all ;)

       
      • Joe D. Velopar

        Joe D. Velopar - 2006-08-17

        >I can basically create them on the spot from the hbm

        I see.

        >So I figured that implementing usertype could lead to a re-usable and cleaner solution, and have a go at that.

        Fair enough.

        > What I'm planning to do is the following:
        > - create a pojo that has a byte[] field;
        > - create a corresponding hbm that has that field as a blobAsByteArray type;
        > - introduce a local class BlobAsByteArray that implements hibernate's UserType;
        > - have that class either use net.sf.beanlib.utils.BlobUtils's methods or
        > Hibernate3BeanTransformer (not sure here).

        You can convert from Blob to byte[] via BlobUtils.toByteArray(Blob), and the inverse via Hibernate.createBlob(byte[]).  Hope this helps.

        Please let me know how this is going, and if you can share the solution when found!

         
    • Jorge E

      Jorge E - 2007-07-23

      Hi .. i'm using Hibernate, Spring in my application and actually using BlobByteArrayType that Spring provide for blob work, now i'm facing the problem when try to clone my object with Hibernate3BeanReplicator, can anybody help on this, please, i'm staring with this lib and i think is great.
      Thanks 

       
      • Joe D. Velopar

        Joe D. Velopar - 2007-07-23

        BlobByteArrayType isn't a JavaBean, so Beanlib won't copy it by default.  However, you can consider plugging in your own CustomBeanTransformerSpi to handle this type.  Have a look at the junit tests for some samples.  Let me know if this works for you.

         
        • Joe D. Velopar

          Joe D. Velopar - 2007-07-23

          Specifically, you can plug in your CustomBeanTransformerSpi via:

              Hibernate3BeanReplicator#initCustomTransformer(CustomBeanTransformerSpi.Factory customTransformerFactory);

           
    • Jorge E

      Jorge E - 2007-07-23

      I have been looking what you told me, but i don't know how to accomplish this, because my POJOS have an byte[], so i don't know the mining of from, toClass in the method isTransform(Object from, Class<T> toClass,PropertyInfo property) of the interface. How can I  enhance to BlobByteArrayType. Sorry but the test unit don't help me so much ..
      Don't you have a sample of a CustomTransormer that i can use has a guide or something ? 

       
      • Joe D. Velopar

        Joe D. Velopar - 2007-07-24

        I have not used BlobByteArrayType before, but it looks like BlobByteArrayType is only specified in your hibernate mapping file, whereas your domain object class is still defined with byte[].  In that case, there should be nothing more you need to do to use Hibernate3BeanReplicator to replicate these objects.  I wonder what specific problem you are encountering.

         
        • Jorge E

          Jorge E - 2007-07-25

          Yes I realize that it shouldn't be a problem, but is not working, seems like in the ReplicatorTemplate class in the replicate method
            if (from.getClass().isArray())
              return beanTransformer.getArrayReplicatable().replicateArray((Object[])from, toClass);
          Is not working for the Array byte[]
          So i implement a CustomBeanTransformerSpi to test this, and when i find a byte[].class then i trying to transform using
          beanTransformer.getArrayReplicatable().replicateArray((Object[])in , toClass);
          and it throw and Exception java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
          And never enter the method replicateArray(V[] array, Class<T> toClass)..
          I'm not sure if i can express correctly the problem, but the truth is that the replication for a byte[] is not working, please, can u test it.

           
          • Joe D. Velopar

            Joe D. Velopar - 2007-07-26

            You are right.  I will be releasing beta8 soon to have this bug fixed.

             
    • Hanson Char

      Hanson Char - 2007-07-26

      Beanlib 3.3.0beta8 is now available for download.  Let me know if this works for you.

       
      • Hanson Char

        Hanson Char - 2007-07-26
         
    • Jorge E

      Jorge E - 2007-07-26

      It works fine, thanks.. by the way great library !!
      We keep in touch ..
      Bye

       

Log in to post a comment.