« Return to Thread: How to avoid retrieving an OneToMany collection?

How to avoid retrieving an OneToMany collection?

by Coby :: Rate this Message:

Reply to Author | View in Thread

I am developing a forum system using JPA+Spring+Struts2. I have 2 entities defined as follows:

public Class Forum{
        ...
        @OneToMany(mappedBy = "forum", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
        private Collection<Topic> topics;
        ...}

public class Topic {
        ...
        @ManyToOne
        private forum forum;
        ...}

I don't wish to retrieve the collection of topics while instantiating a forum, because I think this is bad for the performance since a forum may have many topics and a topic may have many replies, I prefer to use a dedicated query to do the pagination for the topics.

I tried all the ways to avoid retrieving the topic collection including setting fetchType to LAZY, but it will always select all the topics based on the forum_id every time the forum is instantiated...

I also made a lot of search, but it seems there is few discussion on this issue...

I did think of using the namedQuery to select the forum by excluding the topics, but I am not sure if this is the only solution. Is there any better, easier ideas? Thanks.

 « Return to Thread: How to avoid retrieving an OneToMany collection?