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

Hello Bart,

Use the FETCH or LEFT_FETCH query hint instead of the BATCH hints so that the relationship is retrieved in the same statement - forcing the objects to be built from the same row rather than allowing the lazy relationship to use a separate batch query to bring in the values.Â
You can also use JOIN FETCH in your JPQL instead of using a query hint in the same manner.

Best Regards,

On 08/12/2010 10:24 AM, 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:

public class Invoice {
ÂÂÂ @OneToMany(mappedBy = "invoice", fetch=FetchType.LAZY, cascade={CascadeType.REFRESH, CascadeType.MERGE})
ÂÂÂ private List<InvoiceLine> invoiceLines;

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
blog: BK | twitter: @bkummel

_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users