Search code examples
gwtlazy-loadingrequestfactory

Lazy loading with RequestFactory


I'am using GWT 2.4 with Hibernate and RequestFactory and I have queries like this one

contextA.getEntityById(id).with("elements").fire(new Receiver<EntityBaseProxy>() {
        @Override
        public void onSuccess(EntityBaseProxy entity) {
            System.out.println(entity.getElements().size());
  }
});

I'am little confused, when elements, which are the children of the entity, are fetched lazily, I get a NullPointer in System.out.println(entity.getElements().size());


Solution

  • My first solution :

    • I kept the Lazy loading on the entity.getElements()
    • Add a new transient methode that fetch the elements from the DOA, let call it getElementsFromBd()
    • Do the mapping for getElementsFromBd() in the EntityProxy
    • In my presenter I called getElementsFromBd()instead of getElementsFromBd()

    Presenter

    contextA.getEntityById(id).with("elementsFromDb").fire(new Receiver<EntityBaseProxy>() {
        @Override
        public void onSuccess(EntityBaseProxy entity) {
            System.out.println(entity.getElementsFromBd().size());
    }
    });
    

    Entity model

    ...
    @Transient
    public List<Element> getElementsFromDb(){
       doa.getElementsFromDb(this.id);
    }
    ...
    

    Thanks everybody

    EDIT

    I ended up using a servlet that extends RequestFactoryServlet where I begin and commit the transactions

    public class CustomRequestFactoryServlet extends RequestFactoryServlet {
    
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            tx = session.beginTransaction();
            super.service(request, response);
            session.getTransaction().commit();
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
        }
    }
    

    }