4 Replies Latest reply on May 18, 2006 7:10 PM by epbernard

    lazy initialization leads to org.hibernate.LazyInitializatio

    lindner

      I have two entity Beans. Both associated in a OneToMany relation.
      Bean A is The parent bean.
      Bean B is the child bean that holds a reference to A like this:

      class BeanB ....
      private BeanA a;
      @ManyToOne(
      fetch=FetchType.LAZY,
      optional=false
      )
      @JoinColumn(
      name="a",
      nullable=false
      )
      public BeanA getA() {
      return a;
      }

      public void setA(BeanA a) {
      this.a = a;
      }

      Now on a client I retrieve on instance of BeanB. Whe I try to access elements of A like

      BeanB b = controller.find...
      b.getA().getId()

      the followin exception occurs:

      org.hibernate.LazyInitializationException
      could not initialize proxy - no Session
      org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
      at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
      ...

      I understand that BeanB is detached from the transaction context and behind the scene a database transactionis needed to load BeanA. But how can I manage this on a client?
      I tried to put a transaction around the call

      UserTransaction ut = (UserTransaction)(ctx.lookup("UserTransaction"));
      ut.begin();
      BeanB b = controller.find...
      b.getA().getId()
      ut.commit();

      buth the result is the same? What can I do?