Hi, Chris 
     
    An exam paper can have 100+ questions, so I use pagination.  
     
    ChoiceQuestion does not have any relationship defined. Adding a
    ManyToOne relationship to it should work, but there is a possibility
    that we change it to a ManyToMany relationship. 
     
    So I think your last suggestion fits us the most. 
     
    Thanks for your help on this and my previous questions. 
     
    
     
    On 11/8/2011 11:24 PM, Christopher Delahunt wrote:
    Hello
      Warren,
       
       
      Can you describe the relationships you have from ChoiceQuestions? 
      Is there a reason why you are using a query with pagination to
      read in the questions instead of just accessing the relationship
      on the particular Exam?
       
       
      Some options: If you do not need to use the relationships from the
      ChoiceQuestion, you can mark them as lazy so they are only queried
      when needed.  A second option is to not map the relationships from
      the ChoiceQuestion side and instead query for them when needed (as
      you are doing with the Exam->Questions already).
       
       
      A third option is to break up the ChoiceQuestion query into
      chunks, or to read in all the ChoiceQuestion pks and then read in
      the full objects a bunch at a time.  Ie,
       
      "select questions from ExamPaper p join p.questions questions
      where p.id=:paperId and mod(questions.id, 2) = :split"
       
      Or
       
      "select questions.id from ExamPaper p join p.questions questions
      where p.id=:paperId" then
       
        "select questions from ChoiceQuestions questions where
      questions.id in(:listOfIds)"
       
      This would allow you to use joining or batching on any
      relationships in ChoiceQuestion.
       
       
      Best Regards,
       
      Chris
       
       
      On 07/11/2011 10:41 AM, Warren Tang wrote:
       
      But fetch join on OneToMany/ManyToMany
        relationship has problems with
         
        pagination.
         
         
        The problem seems to be called N+1 select problem. I searched
        hard but
         
        could found a solution.
         
         
        It's a problem everyone faces, right? How do you solve it?
         
         
        Regards,
         
        Warren Tang <http://blog.tangcs.com>
         
         
         
        On Friday, November 04, 2011 5:43:32 PM, Leon Derks wrote:
         
        Hello.
           
           
          I think you can do this with a fetch join like:
           
          SELECT pub FROM Publisher pub JOIN pub.magazines mag
           
           
          Leon
           
           
          On Nov 4, 2011, at 10:36 AM, Warren Tang wrote:
           
           
          Hi, everybody
             
             
            I have an ExamPaper entity which has many ChoiceQuestions.
            The
             
            questions are lazy loaded by default (fetch=LAZY).
             
             
            Now I want to get all the quesitions that belong to a
            specific
             
            ExamPaper, so I use the following JPQL:
             
             
            "select p.questions from ExamPaper p where p.id=:paperId";
             
             
            The problem is that questions are "lazy" loaded, so there is
            one
             
            "SELECT" executed for *every single* question. But I want to
            select
             
            them all in one go for performance reasons. What should I
            do?
             
             
            ------------------ Code listing
            -----------------------------------
             
             
            @Entity
             
            public class ExamPaper {
             
            public static final String SELECT_QUESTION_BY_PAPER =
            "select
             
            p.questions from ExamPaper p where p.id=:paperId";
             
             
            @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
             
            private Long id;
             
            private String name;
             
            @OneToMany(fetch = FetchType.LAZY, orphanRemoval = true,
            cascade =
             
            {CascadeType.ALL})
             
            List<ChoiceQuestion> questions;
             
             
            ... ...
             
            }
             
             
            query = em.createQuery(ExamPaper.SELECT_QUESTION_BY_PAPER,
             
            ChoiceQuestion.class);
             
            query.setParameter("paperId", examPaperId);
             
            query.setFirstResult(startPosition);
             
            query.setMaxResults(maxResult);
             
            list = query.getResultList();
             
             
            --
             
            Regards,
             
            Warren Tang <http://blog.tangcs.com/>
             
            _______________________________________________
             
            eclipselink-users mailing list
             
            eclipselink-users@xxxxxxxxxxx
            <mailto:eclipselink-users@xxxxxxxxxxx>
             
            https://dev.eclipse.org/mailman/listinfo/eclipselink-users
             
           
           
           
           
          _______________________________________________
           
          eclipselink-users mailing list
           
          eclipselink-users@xxxxxxxxxxx
           
          https://dev.eclipse.org/mailman/listinfo/eclipselink-users
           
         
        _______________________________________________
         
        eclipselink-users mailing list
         
        eclipselink-users@xxxxxxxxxxx
         
        https://dev.eclipse.org/mailman/listinfo/eclipselink-users
         
       
      _______________________________________________
       
      eclipselink-users mailing list
       
      eclipselink-users@xxxxxxxxxxx
       
      https://dev.eclipse.org/mailman/listinfo/eclipselink-users
       
     
  
 |