Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Join / Batch reading and FetchType.LAZY

Yes can use a LoadGroup to force the loading of an attribute.

    q.setHint(QueryHints.LOAD_GROUP_ATTRIBUTE, "invoiceLines");



Bart Kummel wrote:
> 
> Hi list,
> 
> I'm trying to optimize my application. I've set some relationships to LAZY
> fetching, since we have a lot of UI pages where we don't need the
> referenced
> collection(s). Let's have a look at a simplified example. Say we have the
> following entities:
> 
> @Entity
> public class Invoice {
>     @OneToMany(mappedBy = "invoice", fetch=FetchType.LAZY,
> cascade={CascadeType.REFRESH, CascadeType.MERGE})
>     private List<InvoiceLine> invoiceLines;
> }
> 
> @Entity
> public class InvoiceLine {
>     @ManyToOne
>     @JoinColumn(name = "INV_ID", referencedColumnName = "INV_ID")
>     private Invoice invoice
> }
> 
> I have a named query to get the invoice without the invoice lines. But for
> the UI page where we do need the invoice AND the invoice lines, I want to
> create a named query to get everything as efficient as possible. So I read
> http://java-persistence-performance.blogspot.com/ and decided to go for
> batch reading. So I have code like this:
> 
>     Query q = getEntityManager().createQuery("select i from Invoice i
> where
> i.invoiceId = :invoiceId");
>     q.setParameter("invoiceId", invoiceId);
>     q.setHint(QueryHints.BATCH, "i.invoiceLines");
>     q.setHint("eclipselink.batch.type", "EXISTS");
> 
> I've set the EclipseLink logging to FINE. I can see in the logged SQL
> statements that the data is retrieved from the database as expected.
> However, as soon as the Invoice object gets detached and I call
> getInvoiceLines(), I get the EclipseLink-7242 error, about traversing LAZY
> relationships. I can resolve this by calling getInvoiceLines() directly
> after the query, but I wonder if there's a more elegant way to fix this.
> 
> Best regards,
> Bart Kummel
> 
> 
> -- 
> ___________________________________________________________
> *Bart Kummel*
> 
> Author of the book Apache MyFaces 1.2 Web Application
> Development<http://tinyurl.com/am12wad>
> blog: BK <http://www.bartkummel.net/> | twitter: @bkummel
> <http://twitter.com/bkummel>
> 
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> 
> 


-----
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 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/Join---Batch-reading-and-FetchType.LAZY-tp30406354p30407104.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top