3 Replies Latest reply on Jan 16, 2008 5:25 AM by Carsten Rudat

    Update exiting BLOB does not work

    Carsten Rudat Novice

      Hi all,

      I have a problem updating a existing or null BLOB. I run jboss-4.2.1.GA (all-config) with its EJB3 implementation and MySQL 5.x with mysql-connector-java-5.0.7-bin.jar-driver.

      I have an entity Signature with a unidirectional relationship to Image (which holds the BLOB):

      public class Signature ...
      
       @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
       @JoinColumns({
       @JoinColumn(name="image_id_fk", referencedColumnName="id")
       })
       public com.genloop.base.ImageImpl getImage() {
       return this.image;
       }
      
      
      public class Image ...
      
       @Lob
       @Column(name = "image", columnDefinition="MEDIUMBLOB")
       //@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.NONE) -> I tried this
       public java.sql.Blob getImage() {
       return this.image;
       }
      


      In a SLSB, I try a entityManager.merge(aSignagure) and all changes of Signature and Image (e.g. a field "fileName") will update to the DB, except. The BLOB.

      I have done a little debugging and I saw, that the getImage() was not called after entityManager.merge(). Then I can see something about XYZLoader that (I think so...) fills the entity-bean with the data of the database. This trace calls setImage(Blob) with the old BLOB or (if it was null) with null.

      What could I do?

      Thanks,
      Carsten

        • 1. Re: Update exiting BLOB does not work
          Carsten Rudat Novice

          Hi,

          I tried this with Oracle Express (latest version/very new oracle-driver).

          It doesn't work with Oracle, too, so I think it is

          a) a failure of my code/understanding of merge and cascade
          b) a bug in Hibernate?

          Any ideas - please ;-)

          Carsten

          • 2. Re: Update exiting BLOB does not work
            Carsten Rudat Novice

            Hi all,

            I have tested this with Hibernate directly.

            a) If I set the blob while my entity (Signature) is attached to a EntityManager-Session, my new blob will be updated to the database

            b) If I set the blob while it is not attached, then reattache to a new session and calling a EntityManager.merge(signature) the blob will not be updatet to the database.

            Is a) the only way to update a blob? If I create a new Signature with a Blob (not attached) and call EntityManager.perist(signature), my blob will also be inserted. So, why does update causes these problems?

            Thanks,
            Carsten

            • 3. Re: Update exiting BLOB does not work
              Carsten Rudat Novice

              ...debugging Hibernate - BlobType.replace says:

              public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache) throws HibernateException {
               //Blobs are ignored by merge()
               return target;
              }
              


              "It's not a bug - it's feature"?!