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