Quero fazer uma projeção no atributo de objeto de uma entidade com o Hibernate Criteria 6.
Aqui estão meus modelos:
@Entity
public class MyAuthor {
private String id;
private String name;
private int nbBooks;
private Date birthDate;
@Embedded
private MyAddress address;
}
@Embeddable
public class MyAddress {
private String street;
private MyCity city;
}
@Entity
public class MyCity {
String id;
String name;
}
Aqui está meu teste de unidade:
// We populate data with CITY_1 & CITY_2, ADDR_1 on CITY_1, ADDR_2 on CITY_1 and ADDR_3 on CITY_2 with @Before
CriteriaBuilder criteriaBuilder = sessionFactory.getCurrentSession().getCriteriaBuilder();
CriteriaQuery<MyCity> query = criteriaBuilder.createQuery(MyCity.class);
Root<MyAuthor> root = query.from(MyAuthor.class);
Join<MyAuthor, MyAddress> from = root.join("address", JoinType.LEFT);
// produce this request : select c1_0.id,c1_0.name from MyAuthor ma1_0 left join MyCity c1_0 on c1_0.id=ma1_0.city_id where c1_0.name is not null
query.select(from.get("city"))
.where(criteriaBuilder.isNotNull(from.get("city").get("name")));
// produce the same request : select c1_0.id,c1_0.name from MyAuthor ma1_0 left join MyCity c1_0 on c1_0.id=ma1_0.city_id where c1_0.name is not null
// query.multiselect(from.get("city").get("id"), from.get("city").get("name"))
// .where(criteriaBuilder.isNotNull(from.get("city").get("name")));
// with the query.select(), there are 2 results, with the multiselect there are 3.
List<MyCity> cities = sessionFactory.getCurrentSession().createQuery(query).getResultList();
assertEquals(3, cities.size());
Não entendo como o SQL gerado é o mesmo MAS o comprimento do resultado é diferente.
Alguma ideia? Muito obrigado.