Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Lazy loading performance problem

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
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top