Show TOC

Lazy-Loading Entities with Single-Valued RelationshipsLocate this document in the navigation structure

Use

The JPA specification defines the EAGER and LAZY fetch types.

The SAP implementation of JPA supports lazy loading of single-valued and collection-valued relationships. For downward compatibility with releases prior to SAP NetWeaver 7.3, lazy loading of single-valued relationships must be enabled explicitly, as described below. Otherwise, such entities are loaded eagerly.

Note

Lazy loading is not possible for entities that are part of an inheritance hierarchy.

For more information about fetch types, refer to the Java Persistence API specification.

Prerequisites

You have:

  1. Created a project (for example, JPA, EJB, or Dynamic Web) to store the entities with the Java Persistence facet enabled

    For more information, see Creating JPA Projects in the Developer Studio .

  2. Added the project containing your entities to an Enterprise Application (EAR) project

    For more information, see Assembling Components into Enterprise Applications .

Procedure

Lazy loading of entities with single-valued relationships requires generation of proxy classes for the lazily loadable entities.

  1. In the persistence.xml , enable generation of proxy classes.

                      <property name="com.sap.jpa.execution.generate-proxies" value="true" />
                   
  2. In the application-j2ee-engine.xml , set a reference to the cglib_nodep_22 library.

                      <reference reference-type="hard">
            <reference-target provider-name="sap.com" target-type="library">cglib_nodep_22</reference-target>
    </reference>
                   

    For more information, see Editing the application-j2ee-engine.xml .

Result

The EntityManager.getReference method now returns lazily loadable instances.