I have a @OneToMany
relationship and am using Hibernate 5.3.7 to write an EAGER fetch. I realize EAGER fetching is an anti-pattern, this is a very specific use case. According to this article I can use FetchMode.JOIN
in my TypedQuery
and Hibernate should create a nice query for me, however, I saw it spam a few dozen select statements. It wasn't until I changed this to FetchMode.SUBSELECT
that it condensed the query into one select statement. I realize this is similar to Why Hibernate sometimes ignores FetchMode.JOIN?, however, I don't understand why FetchMode.JOIN
did not work since this is all within a Hibernate query. Any ideas?
Below are the annotations I'm using on the OneToMany
side:
@Entity
@Table(name = "auto", schema = "us")
public class Auto extends AbstractTable {
/** Ordered list of tires. */
@OneToMany(mappedBy = "auto", fetch = FetchType.EAGER, orphanRemoval = true)
@Fetch(FetchMode.SUBSELECT)
private List<Tire> tires;
...
}
I am using SpringBoot with @PersistenceContext
to get my EntityManager
. The Auto has a bi-directional OneToOne
relationship with its containing class AutoOwnerThing.
final String queryString = "FROM AutoOwnerThing e JOIN FETCH e.auto WHERE e.id in :ids";
TypedQuery<AutoOwnerThing> = entityManager.createQuery(queryString, AutoOwnerThing.class);