• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling Lazy Exceptions

 
Jaaouane Aymen
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i have an entity event with a collection of comments, the fetch strategy is lazy because i want to load the comments only when needed.
To avoid the LazyInitialisationException, i write this:
try{
comments=event.getComments();
}
catch(Exception e){
comment.setParameters(eventAction.getEventComments(selectedEvent.getId()));
//eventAction.getEventComments(selectedEvent.getId()) queries the database to get the collection
}
but i think the code is not very nice, do you have a better solution?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your goal is to catch LazyInitializationExceptions, then catch *only* that exception, rather than potentially masking exceptions you're not trying to avoid.

I'm not sure under what circumstances you're trying to avoid it though--you can get the comments when needed by doing a Hibernate.initialize(...) when you need them, and when you don't, just don't intialize them.

Also, please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic