HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaCritical

How to solve the “failed to lazily initialize a collection of role” Hibernate exception

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howhibernatelazilyinitializeroletheexceptionsolvecollectionfailed

Problem

This is the exception:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: mvc3.model.Topic.comments, no session or session was closed

Here is the model:

@Entity
@Table(name = "T_TOPIC")
public class Topic {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name="USER_ID")
    private User author;

    @Enumerated(EnumType.STRING)    
    private Tag topicTag;

    private String name;
    private String text;

    @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL)
    private Collection comments = new LinkedHashSet();

    ...

    public Collection getComments() {
           return comments;
    }

}


The controller, which calls model looks like the following:

@Controller
@RequestMapping(value = "/topic")
public class TopicController {

    @Autowired
    private TopicService service;

    private static final Logger logger = LoggerFactory.getLogger(TopicController.class);
 
    @RequestMapping(value = "/details/{topicId}", method = RequestMethod.GET)
    public ModelAndView details(@PathVariable(value="topicId") int id) {
    
        Topic topicById = service.findTopicByID(id);
        Collection commentList = topicById.getComments();
    
        Hashtable modelData = new Hashtable();
        modelData.put("topic", topicById);
        modelData.put("commentList", commentList);
    
        return new ModelAndView("/topic/details", modelData);       
     }
}


The jsp-page looks li the following:


    
        View Topic
    
    
        
            
                
                ${item.getText()}
            
        
    


The exception is raised when viewing the jsp. In the line with c:forEach loop

Solution

If you know that you'll want to see all Comments every time you retrieve a Topic then change your field mapping for comments to:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "topic", cascade = CascadeType.ALL)
private Collection comments = new LinkedHashSet();


Collections are lazy-loaded by default, take a look at this if you want to know more.

Code Snippets

@OneToMany(fetch = FetchType.EAGER, mappedBy = "topic", cascade = CascadeType.ALL)
private Collection<Comment> comments = new LinkedHashSet<Comment>();

Context

Stack Overflow Q#11746499, score: 252

Revisions (0)

No revisions yet.