6 Replies Latest reply on Jan 30, 2009 5:13 AM by jaikiran

    Lazy byte array gets loaded on query

    agnadello

      Hello,

      I'm trying to specify a property in my entity bean (called Appendix) as lazy loaded by the following annotations:

      @Lob
      @Basic(fetch = FetchType.LAZY)
      @Column(name = "DOCUMENT", nullable = true)
      public byte[] getDocument() {
       return this.document;
      }
      


      What I want is that this property ISN'T loaded unless I access it within the scope of the entity manager. When I search for an Appendix entity bean in the database using a EJB-QL query like:

      // Create traffic type query
      final EntityManager theEntityManager = this.getEntityManager();
      final Query theTrafficTypeQuery = theEntityManager.createQuery("from Appendix a where a.trafficType = :trafficType");
      theTrafficTypeQuery.setParameter("trafficType", theTrafficType);
      final List<Appendix> theAppendixes = theTrafficTypeQuery.getResultList();
      


      I can see in the JBoss log that the property "DOCUMENT" gets loaded anyway.

      select appendix0_.APPENDIX_ID as APPENDIX1_0_, appendix0_.START_DATE as START2_0_, appendix0_.DESCRIPTION as DESCRIPT3_0_, appendix0_.DOCUMENT as DOCUMENT0_, appendix0_.EXTENSION_ID as EXTENSION9_0_, appendix0_.CREATE_DATE as CREATE5_0_, appendix0_.PAGE_ID as PAGE6_0_, appendix0_.PRODUCT_ID as PRODUCT7_0_, appendix0_.STOP_DATE as STOP8_0_ from APPENDIX appendix0_ where appendix0_.PRODUCT_ID=?
      


      Does anyone know how I get my byte array property lazy loaded?

      If I understand it correct, loading lazy loaded entities should use the EntityManager method getReference(...), but in my case I load the entities using a query.

      In the following Hibernate reference docs I've read something about instrumentation, is it really necessary?

      http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#performance-fetching-lazyproperties.

      I've really tried to search the forum but doesn't find anything useful.

      I'm using JBoss 4.0.4 GA and EJB3.0RC6.

      Regards, Andreas

        • 1. Re: Lazy byte array gets loaded on query
          stnor

          Hi Andreas,
          It's really necessary, but unfortunately, I didn't manage to get the byte code build-time instrumentation to help either. I currently use non-FK id:s for blobs as a workaround, which sucks.

          I think @Lob and FetchType.LAZY is broken.

          Stefan

          • 2. Re: Lazy byte array gets loaded on query
            agnadello

            Hi Stefan and thanks for your answer!

            So, creating another table for the blob and referencing it from my Appendix entity is the way to do it right now? That sucks! :-(

            Well, I hope they fix it soon...

            Thank you anyways!

            Regards, Andreas

            • 3. Re: Lazy byte array gets loaded on query
              agnadello

              Solved it!

              Using a java.sql.Blob instead of byte array.

              Strange, because the docs says you could use both?!

              Anyways, problem has vanished :-)

              Cheers!

              • 4. Re: Lazy byte array gets loaded on query
                dguralnik

                Hello,

                I've got the same problem. But switching to a Blob attribute didn't help:
                it is still loading BLOB column in a query!

                I played with hibernate.bytecode.use_reflection_optimizer but got the same result.

                Another idea was calling hibernate's javassist instrument task during building the application. It affected the exception during deploying:

                javax.persistence.PersistenceException: org.hibernate.MappingException: Could no
                t determine type for: org.hibernate.bytecode.javassist.FieldHandler, for columns
                : [org.hibernate.mapping.Column(fieldHandler)]
                at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Con
                figuration.java:698)


                Any ideas?

                Thank you.

                • 5. Re: Lazy byte array gets loaded on query
                  deanouk

                  We're also puzzled by this.

                  Even with the lazy load and lob annotation a byte array is still eagerly loaded...

                  • 6. Re: Lazy byte array gets loaded on query
                    jaikiran

                    I guess you might find some help in the Hibernate forums, because JBoss JPA uses Hibernate as its implementation.