1 Reply Latest reply on Dec 30, 2005 8:58 PM by Emmanuel Bernard

    EntityManager tuning to get only a few Items from a nested c

    Martin Heinemann Newbie

      Hi,


      today i ran into a new problem.
      I have a n2m relation between to tables.
      class DrugCategory <-- n2m --> class Drug

      if i obtain one DrugCategory with the EntityManager and want to load
      the nested Drug objects to that Category JBoss has a lot of work to do, because there are nealry 10000 of the nested Drug-Objects.
      I do Lazy-Loading and want only to get about the first 20 of the nested Drug-Objects.
      How can i get this way?

      I built a loop like

       DrugCategory cat = em.find(DrugCategory.class, catId);
      
       Collection <Drug>dc = cat.getDrugs();
       Collection <Drug> newDc = new ArrayList<Drug>();
       int i = 0;
       for (Iterator iter = dc.iterator(); iter.hasNext() && (i < 10);i++) {
       newDc.add((Drug) iter.next());
       }
      


      the loop should end after 10 cyles. It might do that, but Jboss is still reading _all_ the nested objects from the database.

      How can i achieve this?

      thanks for your help

      martin