10

I have a Parent entity with a @OneToMany relationship with a Child entity. Most of the time, when I need to work with a Parent’s Child entities, I’m working with a single parent, so lazy fetching (FetchMode.SELECT) is appropriate.

However, I have a situation where I’m querying a large number of Parents (sometimes hundreds or even thousands), and I need to work with their Child entities. FetchMode.SELECT gives me a serious N+1 problem, so I need to do something different in this scenario. If I were doing this via JDBC, it’d be single query for the Parent records, then another query for all the Child records using an IN statement (where child.parentid in (?,?,?....)). I need live Hibernate entities, because Hibernate Search is going to call getChildren() as part of its indexing process.

The options I’ve considered are:

  1. Criteria.setFetchMode(“children”, FetchMode.JOIN) (or join fetch in HQL) - this would give me a cartesian product, though, which is brutal with that many entities.
  2. Adding @BatchSize to Parent.getChildren() - this would help for my big batch scenario, but it isn’t really the strategy I want to use for normal operations. It’d be perfect if I could set a batch size for the fetch in my Criteria/HQL, but I can’t find a way to do so.
  3. Using FetchMode.SUBSELECT in Parent.getChildren() - much like @BatchSize, this would be great for my big batch scenario, but isn’t appropriate for normal operations, and I can’t find a way to use it with Criteria/HQL (Criteria and the entity annotations use different FetchMode enums, despite the duplicate name).

tldr; I have a one-to-many relationship with a lazy fetch mode, but sometimes I want to be able to efficiently load the relationship for many entities at once.

4
  • 1. I don't see what prevents you from doing the same IN query with JPA than the one you would do with JDBC; 2. I don't see why the cartesian product would be so brutal. Suppose you have 500 parents each with 10 children, the IN query would load 5000 rows, and the join fetch query would load... 5000 rows.
    – JB Nizet
    Commented Nov 3, 2015 at 20:01
  • 1. Hibernate Search needs Parent.getChildren() to container the child records, which is why I can't just do a separate query for the children. I've updated the question to clarify this. 2. The problem isn't the number of rows, it's retrieving all the data for the same Parent multiple times (it has a non-trivial number of columns) Commented Nov 3, 2015 at 20:06
  • IMHO when the query result sizes become even modestly large and/or the query becomes even modestly complex, I just use a native query via createNativeQuery() and be done with it. Hibernate is convenient to use, but can quickly become inefficient.
    – Bohemian
    Commented Nov 3, 2015 at 20:11
  • Hah, I feel the same way. Sadly, we're indexing with Hibernate Search, and I need to send it real Parent entities, and it's going to call getChildren(). So I need a way to efficiently populate that relationship or I'll be in "N+1 Land" (Disney World's least popular park) Commented Nov 3, 2015 at 20:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.