Lazy Loading Collection From Database.

Hi All,

I would start this post by saying “please help” becouse I really cannot solve my problem of getting data by using Lazy approach. I’m using JPAContainer and some Entities Class which have some ManyToOne and OneToMany reletioships. Someone have any Idea about how to tell my Entity Provider to load a collection annotated with Lazy ??. If I use EAGER It all works but this is a very poor approach since the performance become extremly bad. As an Exmple:



@Entity
@Table(name = "company_employee", catalog = "greeenvision", schema = "")
@NamedQueries({
    @NamedQuery(name = "CompanyEmployee.findAll", query = "SELECT c FROM CompanyEmployee c")})
public class CompanyEmployee implements Serializable {

        .....

     @JoinColumn(name = "parent_employee", referencedColumnName = "id")
     @ManyToOne(fetch = FetchType.EAGER)
      private CompanyEmployee parentEmployee;
        .....

     @OneToMany(mappedBy = "parentEmployee", fetch = FetchType.LAZY)
      private Set<CompanyEmployee> companyEmployeeSet;

}

and my entity provider is:



public abstract class LocalEntityProviderBean<T> extends CachingBatchableLocalEntityProvider<T> {

    @PersistenceContext
    private EntityManager em;
    protected final Logger logger = Logger.getLogger(getClass());
    private final Class<T> entityType;

    protected LocalEntityProviderBean(Class<T> entityClass) {
        super(entityClass);
        setCacheInUse(true);
        entityType = entityClass;
        setEntityCacheMaxSize(10000);
        setTransactionsHandledByProvider(false);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public T updateEntity(T entity) {
        if (logger.isDebugEnabled()) {
            logger.debug("Updating entity [" + entity + "]
");
        }
        return super.updateEntity(entity);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public T addEntity(T entity) {
        if (logger.isDebugEnabled()) {
            logger.debug("Adding entity [" + entity + "]
");
        }
        return super.addEntity(entity);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void removeEntity(Object entityId) {
        if (logger.isDebugEnabled()) {
            logger.debug("Removing entity identified by [" + entityId + "]
");
        }
        super.removeEntity(entityId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateEntityProperty(Object entityId, String propertyName,
            Object propertyValue) throws IllegalArgumentException {
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Updating property [" + propertyName + "]
 of entity "
                    + "identified by [" + entityId + "]
 to [" + propertyValue + "]
");
        }
        super.updateEntityProperty(entityId, propertyName, propertyValue);
    }

    @PostConstruct
    public void init() {
        Assert.notNull(em, "no entity manager has been specified");
        if (logger.isInfoEnabled()) {
            logger.info("Initializing entity provider bean");
        }
        setEntityManager(em);
        /*
         * The entity manager is transaction scoped, which means that the
         * entities will be automatically detached when the transaction is closed.
         * Therefore, we do not need to explicitly detach them.
         */
        setEntitiesDetached(false);
    }

    public JPAContainer<T> getNewDefaultContainer() {
        JPAContainer<T> entityContainer = new JPAContainer<T>(entityType);
        entityContainer.setEntityProvider(this);
        entityContainer.setContainsIdFiresItemSetChangeIfNotFound(true);
        entityContainer.removeContainerProperty("version");
        return entityContainer;
    }
}

Any Idea?? Thanks Guys!