|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
How to avoid retrieving an OneToMany collection?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. |
|
|
Re: 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.
James Sutherland EclipseLink, TopLink Wiki: EclipseLink, TopLink Forums: TopLink, EclipseLink Book: Java Persistence |
|
|
Re: How to avoid retrieving an OneToMany collection?Would weaving be required for a OneToOne lazy relationship to work ? It relates to another thread we've been discussing for a couple of days
Thanks Richard
|
|
|
Re: How to avoid retrieving an OneToMany collection?Yes, weaving is required for OneToOne and ManyToOne lazy.
James Sutherland EclipseLink, TopLink Wiki: EclipseLink, TopLink Forums: TopLink, EclipseLink Book: Java Persistence |
| Free embeddable forum powered by Nabble | Forum Help |