2 Replies Latest reply on Sep 22, 2005 12:07 PM by ibsscott

    FetchType.LAZY

    ibsscott Newbie

      I'm not sure that FetchType.LAZY is working correctly or perhaps I do not fully understand how it works. I have an ejb with a OneToMany relationship. I really just want the relationship in order to use the Cascade capabilities and only fetch the relationship when I require it. I'm trying to create an xml document from the bean and do not necessarily want the relationships included.

      I saw that there was FetchType.LAZY bug recently fixed and I am using the CVS version which I downloaded on 9/20/2005.

      ...
       @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
       @JoinColumn
       public List<EmployeeEJB> getEmployee() {
       return this._getEmployee();
       }
      ...


      When I fetch the ejb and try to marshall it into xml, the related beans are included in the output.

      CompanyEJB company = em.find(CompanyEJB.class, new String("1") );
       CompanyDocument companyDocument = objFactory2.createCompanyDocument();
       companyDocument.setCompany(company);
       Marshaller m = jc.createMarshaller();
       m.marshal(companyDocument, System.out);


      I thought perhaps the Marshaller was somehow retrieving the relationship so I cloned the bean prior to marshalling but I still get the relationships included in the output.

      CompanyEJB company = em.find(CompanyEJB.class, new String("1") );
      // clone
       CompanyEJB companyClone = (CompanyEJB) company.clone();
      //
       CompanyDocument companyDocument = objFactory2.createCompanyDocument();
       companyDocument.setCompany(companyClone);
       Marshaller m = jc.createMarshaller();
       m.marshal(companyDocument, System.out);

      Output:

      <company>
      ...
       <employee>
       <employeeName> employee name goes here </employeeName>
       </employee>
       <companyName> company name goes here </companyName>
      ...
      <company>