Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] How to avoid retrieving an OneToMany collection?

If you set the fetch to LAZY the OneToMany will not be read until you access
it.  Ensure that you do not access the relationship in your code.  Also
ensure that you are using weaving (not required for OneToMany LAZY, but
required for ManyToOne LAZY).

If you never intend to read the topics, then you are better off not mapping
it, and just querying it.

Coby wrote:
> 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.

--- James Sutherland
 EclipseLink ,
Wiki: EclipseLink , TopLink 
Forums: TopLink , EclipseLink 
Book: Java Persistence 
View this message in context:
Sent from the EclipseLink - Users mailing list archive at

Back to the top