I am having trouble selecting embedded fields from union sub queries wrapped in CriteriaQuery.
Getting following error when tried to execute below code snippet. Am I doing in right way?
Pls clarify.
java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.EmbeddableValuedModelPart.findSubPart(String, org.hibernate.metamodel.mapping.EntityMappingType)" because "modelPartContainer" is null
final HibernateCriteriaBuilder cb = entityManager.unwrap(Session.class).getCriteriaBuilder();
var wrapperQuery = cb.createTupleQuery();
var subquery1 = wrapperQuery.subquery(Tuple.class);
var root1 = subquery1.from(Person.class);
subquery1.multiselect(
root1.get("id").alias("id"),
root1.get("firstName").alias("firstName"),
root1.get("lastName").alias("lastName"),
root1.get("email").alias("email"),
root1.get("phone").alias("phone"),
root1.get("address").alias("address"))
.where(cb.equal(root1.get("address").get("city"), "NYC"));
var subquery2 = wrapperQuery.subquery(Tuple.class);
var root2 = subquery2.from(Person.class);
subquery2.multiselect(
root2.get("id").alias("id"),
root2.get("firstName").alias("firstName"),
root2.get("lastName").alias("lastName"),
root2.get("email").alias("email"),
root2.get("phone").alias("phone"),
root2.get("address").alias("address"))
.where(cb.like(root2.get("lastName"), "ba%"));
var unionQuery = cb.union(subquery1, subquery2);
var wrapperRoot = wrapperQuery.from(unionQuery);
wrapperQuery.multiselect(
wrapperRoot.get("id").alias("id"),
wrapperRoot.get("email").alias("email"),
wrapperRoot.get("address").alias("address")
).orderBy(cb.desc(cb.literal(1)));
List<Tuple> tuples = entityManager.createQuery(wrapperQuery).setMaxResults(5).getResultList();
System.out.println(tuples);
Thanks