4 Replies Latest reply on Feb 14, 2006 9:33 AM by ejb3workshop

    Load entity lazy fields...

    cspada

      Hi,

      i'm using some entities with a lot of relations and tagged them with the LAZY FetchType.
      So i decided to use masks to load the needed relations from a session bean.
      See below :
      public Account loadRelations(int mask) {
      Logger logger = Logger.getLogger(this.getClass());
      if (mask != 0) {
      // Set vehicleValue object if requested.
      if ((mask & RelationsMasks.ACCOUNT_INCLUDE_VEHICLE_MASK) != 0) {
      getVehicle();
      }
      // Set moduleValue object if requested.
      if ((mask & RelationsMasks.ACCOUNT_INCLUDE_MODULE_MASK) != 0) {
      getModule();
      }
      // Set fleetValue object if requested.
      if ((mask & RelationsMasks.ACCOUNT_INCLUDE_FLEET_MASK) != 0) {
      getFleets().size();
      }
      }

      So the issue is that a getVehicle() does not load the relation and leads to a LazyException.
      getFleets().size() works.
      and getVehicle().getId() does not.

      If i write those in a logger it works.
      But this solution is not very clean.

      Is there a way, as with hibernate if i can remember to load the relation with a method call ?
      Or any other way please.

      Christophe.

        • 1. Re: Load entity lazy fields...
          ejb3workshop

          There is the @Fetch annoation which might solve your problem.

          • 2. Re: Load entity lazy fields...
            cspada

            Thanks, but i couldn't find anything about the @Fetch annotation and how to use it.
            It doesn't seem to be part of the ejb specs. Is it a specific Hibernate tag ?
            Could you give me a link or a piece of example ?

            Thank you.
            Christophe.

            • 3. Re: Load entity lazy fields...
              wesslan
              • 4. Re: Load entity lazy fields...
                ejb3workshop

                Sorry, it was late when I made the last post. I was thinking about the Fetch Joins.



                4.4.5.3 Fetch Joins
                An important use case for LEFT JOIN is in enabling the prefetching of related data items as a side effect
                of a query. This is accomplished by specifying the LEFT JOIN as a FETCH JOIN.
                A FETCH JOIN enables the fetching of an association as a side effect of the execution of a query. A
                FETCH JOIN is specified over an entity and its related entities.
                The syntax for a fetch join is
                fetch_join ::= [ LEFT [OUTER] | INNER ] JOIN FETCH join_association_path_expression
                The association referenced by the right side of the FETCH JOIN clause must be an association that
                belongs to an entity that is returned as a result of the query. It is not permitted to specify an identification
                variable for the entities referenced by the right side of the FETCH JOIN clause, and hence references
                to the implicitly fetched entities cannot appear elsewhere in the query.
                The following query returns a set of departments. As a side effect, the associated employees for those
                departments are also retrieved, even though they are not part of the explicit query result. The persistent
                fields or properties of the employees that are eagerly fetched are fully initialized. The initialization of
                the relationship properties of the employees that are retrieved is determined by the metadata for the
                Employee entity class.


                SELECT d
                FROM Department d LEFT JOIN FETCH d.employees
                WHERE d.deptno = 1



                A fetch join has the same join semantics as a left outer join, except that the related objects specified on
                the right-hand side of the join operation are not returned in the query result or otherwise referenced in
                the query. Hence, for example, if department 1 has five employees, the above query returns five references
                to the department 1 entity.


                Extract from ejb-3_0-pfd-spec-persistence.pdf