Критерии Hibernate Criteria 6 Проекция на вложенное объект странное поведениеJAVA

Программисты JAVA общаются здесь
Anonymous
Критерии Hibernate Criteria 6 Проекция на вложенное объект странное поведение

Сообщение Anonymous »

Я хочу сделать проекцию на атрибуте объекта объекта с критериями Hibernate 6. < /p>
Вот мои модели: < /p>
@Entity
public class MyAuthor {
private String id;
private String name;
private int nbBooks;
private Date birthDate;
@Embedded
private MyAddress address;
}
< /code>
@Embeddable
public class MyAddress {
private String street;
private MyCity city;
}
< /code>
@Entity
public class MyCity {
String id;
String name;
}
< /code>
Here is my unit test :
// 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 query = criteriaBuilder.createQuery(MyCity.class);
Root root = query.from(MyAuthor.class);
Join 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 cities = sessionFactory.getCurrentSession().createQuery(query).getResultList();

assertEquals(3, cities.size());
< /code>
I do not understand how the generated SQL is the same BUT the length of result is different.
Any ideas ?
Many thanks.

Подробнее здесь: https://stackoverflow.com/questions/795 ... -behaviour

Вернуться в «JAVA»