The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » GlassFish

Thread: Exception [TOPLINK-7242] - How do I instantiate LAZY relationship?

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 3 - Last Post: Apr 12, 2007 11:12 AM by: swmk
cayhorstmann

Posts: 228
Exception [TOPLINK-7242] - How do I instantiate LAZY relationship?
Posted: Aug 25, 2006 10:40 AM
  Click to reply to this thread Reply

I am getting this error message:

Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, instantiate the LAZY relationship prior to serialization.

Indeed, I export an entity from a session bean to JSF, and a part of it does not have FetchType.EAGER. (That would ruin performance elsewhere). But I did attempt to fetch the relationship in two ways, neither of which worked.

Here is my scenario. A Submission has one Quiz. A Quiz has many Question entities. I use this query:

Query q = em.createQuery("SELECT DISTINCT x FROM Submission x JOIN x.quiz AS q JOIN FETCH q.questions WHERE x.submitter.id = :userId AND x.quiz.id = :quizId")
.setParameter("userId", userId)
.setParameter("quizId", quizId);

No dice. Is that not the right syntax for the fetch join? (I previously tried JOIN FETCH x.quiz.questions, and that gave me a syntax error, as it should, according to the grammar in the JPA spec. Too bad, actually, because that expresses succinctly what I want to achieve.)

I also tried accessing the collection entries before returning the object. (I logged the question titles.) Again, no dice. Why didn't that instantiate the LAZY relationship?

Is there another way of "instantiating a LAZY relationship"?

Thanks,

Cay

BTW, could the implementor of Exception [TOPLINK-7242] favor us with telling us WHICH relationship caused the problem?

tware

Posts: 59
Re: Exception [TOPLINK-7242] - How do I instantiate LAZY relationship?
Posted: Aug 29, 2006 11:03 AM   in response to: cayhorstmann
  Click to reply to this thread Reply

Can you provide the snipped of code that causes the issue?

Can you also provide the full stack trace?

Thanks.

cayhorstmann

Posts: 228
Re: Exception [TOPLINK-7242] - How do I instantiate LAZY relationship?
Posted: Sep 8, 2006 5:55 PM   in response to: tware
  Click to reply to this thread Reply

Sure. Here is the JSF managed bean:

public class QuizMB {
@EJB
private QuizSB quizSB;
public String takeQuiz() {
Submission currentSubmission = quizSB.getSubmission(userId, quizId);
currentQuiz = currentSubmission.getQuiz();
currentQuestion = currentQuiz.getQuestions().get(0); // blows up with LAZY error
}
}

Here is the session bean:

public Submission getSubmission(Long userId, Long quizId) {
Query q = em.createQuery("SELECT DISTINCT x FROM Submission x JOIN x.quiz AS q LEFT JOIN FETCH q.questions WHERE x.submitter.id = :userId AND q.id = :quizId")
.setParameter("userId", userId)
.setParameter("quizId", quizId);
return (Submission) q.getSingleResult();
}

The data model is simple. A Submission has a Quiz and a submitter (a Person entity). A Quiz has many Question entities.

The query returns the correct submission, but it fails to fetch the associated Question entities.

If you think that this should work, I'll take the time to make a minimal example and file an issue.

swmk

Posts: 6
Re: Exception [TOPLINK-7242] - How do I instantiate LAZY relationship?
Posted: Apr 12, 2007 11:12 AM   in response to: cayhorstmann
  Click to reply to this thread Reply

what about trying currentQuiz.getQuestions().size() before currentQuiz.getQuestions().get(0)?




 XML java.net RSS