Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » BatchFetch examples?(Having issues getting BatchFetch to work)
BatchFetch examples? [message #1758547] Thu, 30 March 2017 02:09 Go to next message
Jason Mesches is currently offline Jason MeschesFriend
Messages: 4
Registered: March 2017
Junior Member
We're using JPA and Hibernate and are using a repository pattern. At the repo class, we've defined a query to fetch a list of CustomerOrder entities:

@Query("SELECT DISTINCT co FROM CustomerOrder co " +
           "INNER JOIN co.customerOrderItems coi " +
           "WHERE co.customerOrderTypeId IN (1,3,8) " +
           "AND co.customerOrderStatusId IN (2,6) " +
           "AND co.customerEmail IS NOT NULL " +
           "AND coi.performanceId = :performanceId")
List<CustomerOrder> findAllOrdersWithActiveTicketsForPerformance(@Param("performanceId") BigDecimal performanceId);


Because there can be many thousands of these we need to process, and several relationships (some of which are nested), we'd like to avoid the dread n+1 scenario.

If I can get just one of these relationships to work, I'm pretty sure I can do the rest! Here's what we have to link CustomerOrder to CustomerOrderItem.

In CustomerOrderItem (the child entity):
@Entity
@Table(name = "CUSTOMER_ORDER_ITEM")
public class CustomerOrderItem
{
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="CUSTOMER_ORDER_ID")
    private CustomerOrder customerOrder;


In CustomerOrder (the parent entity):
@Entity
@Table(name = "CUSTOMER_ORDER")
public class CustomerOrder
{
    @OneToMany(mappedBy = "customerOrder", fetch = FetchType.EAGER)
    @BatchFetch(BatchFetchType.EXISTS)
    private List<CustomerOrderItem> customerOrderItems;


If I leave CustomerOrder.CustomerOrderItems as EAGER I wind up with a bazillion queries, which I would expect if I use EAGER, however I've read answers to questions that swear it must be set to EAGER

If I change to LAZY, I receive a LazyInitializationException. Not sure what I'm doing wrong. Seems like this should be straightforward, but documentation is wrong in some cases, or so sparse that it has one or two lines completely out-of-context and I'm not sure how to apply to our situation.

Anyone have ideas or a snippet of "real world" code I can look at and understand how to make this work?

Thanks,
---Jason
Re: BatchFetch examples? [message #1758683 is a reply to message #1758547] Fri, 31 March 2017 15:57 Go to previous messageGo to next message
Eric Rizzo is currently offline Eric RizzoFriend
Messages: 3070
Registered: July 2009
Senior Member
These forums are for topics specific to using Eclipse or any of the open-source projects hosted by eclipse.org - they are not for general programming help.
Re: BatchFetch examples? [message #1758689 is a reply to message #1758683] Fri, 31 March 2017 16:33 Go to previous messageGo to next message
Jason Mesches is currently offline Jason MeschesFriend
Messages: 4
Registered: March 2017
Junior Member
Ummm.... BatchFetch *is* specific to Eclipse, yes?. That's why I'm here... in this forum... asking for assistance with BatchFetch... which is the title of my question.
Re: BatchFetch examples? [message #1758694 is a reply to message #1758689] Fri, 31 March 2017 16:56 Go to previous messageGo to next message
Eric Rizzo is currently offline Eric RizzoFriend
Messages: 3070
Registered: July 2009
Senior Member
My mistake, I thought it was standard JPA.
I'll move this to the EclipseLink forum where it might get more attention.
Re: BatchFetch examples? [message #1758695 is a reply to message #1758694] Fri, 31 March 2017 16:57 Go to previous messageGo to next message
Jason Mesches is currently offline Jason MeschesFriend
Messages: 4
Registered: March 2017
Junior Member
Ok, cool. Didn't see one specifically for EclipseLink. Thanks for moving it!
Re: BatchFetch examples? [message #1758972 is a reply to message #1758695] Tue, 04 April 2017 20:56 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
This is an Eclipse forum which covers EclipseLink - an alternative JPA provider to Hibernate. Since you are using Hibernate, your EclipseLink specific annotations are not going to be used.
Note, EclipseLink does not throw LazyInitializationException; as long as the EMF is open and the Entity hasn't been serialized, EclipseLink will fetch the data for you.

I'd recommend you try EclipseLink via <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> in your persistence.xml.
Re: BatchFetch examples? [message #1759056 is a reply to message #1758972] Wed, 05 April 2017 16:07 Go to previous message
Jason Mesches is currently offline Jason MeschesFriend
Messages: 4
Registered: March 2017
Junior Member
Ah, my misunderstanding for how Eclipse fits in -- or doesn't in our case. Thanks for the reply. I used my second choice solution to pre-fetch in the meantime: reworked the entities and queries to fetch everything I need up-front instead of on demand.

Thanks for responding,
---J
Previous Topic:WLS 12.2.1 with TopLink and weird Exception
Next Topic:EntityNotFound Esxception or Null values ?
Goto Forum:
  


Current Time: Tue Mar 19 03:37:58 GMT 2024

Powered by FUDForum. Page generated in 0.02405 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top