use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.
the class CriteriaTest method test_criteria_typedquery_expression_example.
@Test
public void test_criteria_typedquery_expression_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<String> criteria = builder.createQuery(String.class);
Root<Person> root = criteria.from(Person.class);
criteria.select(root.get(Person_.nickName));
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
List<String> nickNames = entityManager.createQuery(criteria).getResultList();
assertEquals(1, nickNames.size());
});
}
use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.
the class CriteriaTest method test_criteria_group_by_example.
@Test
public void test_criteria_group_by_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
Root<Person> root = criteria.from(Person.class);
criteria.groupBy(root.get("address"));
criteria.multiselect(root.get("address"), builder.count(root));
List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
for (Tuple tuple : tuples) {
String name = (String) tuple.get(0);
Long count = (Long) tuple.get(1);
}
assertEquals(2, tuples.size());
});
}
use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.
the class CriteriaTest method test_criteria_typedquery_entity_example.
@Test
public void test_criteria_typedquery_entity_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
criteria.select(root);
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
List<Person> persons = entityManager.createQuery(criteria).getResultList();
assertEquals(1, persons.size());
});
}
use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.
the class CriteriaTest method test_criteria_from_join_example.
@Test
public void test_criteria_from_join_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Phone> criteria = builder.createQuery(Phone.class);
Root<Phone> root = criteria.from(Phone.class);
Join<Phone, Person> personJoin = root.join(Phone_.person);
Join<Person, String> addressesJoin = personJoin.join(Person_.addresses);
criteria.where(builder.isNotEmpty(root.get(Phone_.calls)));
List<Phone> phones = entityManager.createQuery(criteria).getResultList();
assertEquals(2, phones.size());
});
}
use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.
the class CriteriaTest method test_criteria_tuple_example.
@Test
public void test_criteria_tuple_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
Root<Person> root = criteria.from(Person.class);
Path<Long> idPath = root.get(Person_.id);
Path<String> nickNamePath = root.get(Person_.nickName);
criteria.multiselect(idPath, nickNamePath);
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
for (Tuple tuple : tuples) {
Long id = tuple.get(idPath);
String nickName = tuple.get(nickNamePath);
}
for (Tuple tuple : tuples) {
Long id = (Long) tuple.get(0);
String nickName = (String) tuple.get(1);
}
assertEquals(1, tuples.size());
});
}
Aggregations