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.
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/How-to-avoid-retrieving-an-OneToMany-collection--tp19728719p19744670.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top