This is a simple Product entity that refers to a subgroup:
public class Product implements Comparable<Product> {
...
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@NotNull
private ProductSubGroup productSubGroup;
...
}
I have a map containing Product
's in another entity:
public class FinishedProduct {
...
@NotNull
@ManyToOne
private Product product;
@ElementCollection(fetch=FetchType.LAZY)
@MapKeyJoinColumn
@Column(name="amount")
@Sort(type=SortType.NATURAL)
@Fetch(FetchMode.SUBSELECT)
private SortedMap<Product, Double> byproducts = new TreeMap<>();
...
}
I can load the map with this code:
Root<FinishedProduct> root = q.from(FinishedProduct.class);
root.fetch("product", JoinType.LEFT);
root.fetch("byproducts", JoinType.LEFT);
This works, but I need the productSubGroup
of the byproducts stored in the map without generating n+1 selects. How can I fetch them? Just adding the fetch to the end results in an exception:
root.fetch("byproducts", JoinType.LEFT).fetch("productSubGroup", JoinType.LEFT);
org.springframework.dao.InvalidDataAccessApiUsageException:
Collection of values [null] cannot be source of a fetch
Also tried to fool around with MapJoin
, same exception:
MapJoin<FinishedProduct,Product,Double> map = root.joinMap("byproducts", JoinType.LEFT);
map.fetch("productSubGroup", JoinType.LEFT);
I guess I somehow need to refer to the map key, but no idea how.