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

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