1 Reply Latest reply on Apr 11, 2006 8:07 PM by Alexander Hartner

    Force LAZY loading on One-One relationship

    Alexander Hartner Expert

      Between ObjectA and ObjectB there is a One-One relationship, which is configured as LAZY.

      @OneToOne(mappedBy="objectA",fetch=FetchType.LAZY,cascade={CascadeType.MERGE,CascadeType.PERSIST}) @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL,org.hibernate.annotations.CascadeType.DELETE_ORPHAN } )
      public ObjectB getB()
       return b;

      When I load / find ObjectA via this finder method

      public ObjectA findA(int id)
       logger.info("Find A by id");
       ObjectA objectA = manager.find(ObjectA.class, id);
       return objectA;

      I can see the following output in the log file:

      10:25:51,718 INFO [ServiceProviderBean] Find A by id
      10:25:51,718 INFO [STDOUT] Hibernate: select objecta0_.id as id141_0_, objecta0_.name as name141_0_, objecta0_.version as version141_0_ from ObjectA objecta0_ where objecta0_.id=?
      10:25:51,718 INFO [STDOUT] Hibernate: select objectb0_.id as id142_0_, objectb0_.name as name142_0_, objectb0_.version as version142_0_, objectb0_.imagedata as imagedata142_0_, objectb0_.objectA_id as objectA5_142_0_ from ObjectB objectb0_ where objectb0_.objectA_id=?
      10:25:51,750 INFO [ObjectB] Post Load
      10:25:51,750 INFO [STDOUT] Hibernate: select objectc0_.id as id143_0_, objectc0_.name as name143_0_, objectc0_.version as version143_0_, objectc0_.imagedata as imagedata143_0_, objectc0_.objectA_id as objectA5_143_0_ from ObjectC objectc0_ where objectc0_.objectA_id=?
      10:25:51,765 INFO [ObjectC] Post Load
      10:25:51,765 INFO [ObjectA] Post Load

      Even though the relationship is configured as LAZY it seems that the related objects are loaded. In this case as ObjectB contains a BLOB this is a very expensive operation.

      I understand that the specifications specify a default EAGER mapping for a One-One relationship, but there should be the option to make it LAZY.

      Yes I know that I previously complained about the same thing on One-Many relationships.
      That was a mistake, I had the relationship wrong. It should have been a One-One rather then a One-Many.[/url]