use of javax.persistence.Tuple in project hibernate-orm by hibernate.
the class CriteriaTest method test_criteria_from_multiple_root_example.
@Test
public void test_criteria_from_multiple_root_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
String address = "Earth";
String prefix = "J%";
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
Root<Person> personRoot = criteria.from(Person.class);
Root<Partner> partnerRoot = criteria.from(Partner.class);
criteria.multiselect(personRoot, partnerRoot);
Predicate personRestriction = builder.and(builder.equal(personRoot.get(Person_.address), address), builder.isNotEmpty(personRoot.get(Person_.phones)));
Predicate partnerRestriction = builder.and(builder.like(partnerRoot.get(Partner_.name), prefix), builder.equal(partnerRoot.get(Partner_.version), 0));
criteria.where(builder.and(personRestriction, partnerRestriction));
List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
assertEquals(2, tuples.size());
});
}
use of javax.persistence.Tuple in project hibernate-orm by hibernate.
the class TreatListJoinTest method testTreatJoin.
@Test
public void testTreatJoin() {
EntityManager em = createEntityManager();
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = cb.createTupleQuery();
final Root<TestEntity> testEntity = query.from(TestEntity.class);
final List<Selection<?>> selections = new LinkedList();
selections.add(testEntity.get("id"));
final ListJoin<TestEntity, AbstractEntity> entities = testEntity.joinList("entities", JoinType.LEFT);
entities.on(cb.equal(entities.get("entityType"), EntityA.class.getName()));
final ListJoin<TestEntity, EntityA> joinEntityA = cb.treat(entities, EntityA.class);
selections.add(joinEntityA.get("id"));
selections.add(joinEntityA.get("valueA"));
final ListJoin<TestEntity, AbstractEntity> entitiesB = testEntity.joinList("entities", JoinType.LEFT);
entitiesB.on(cb.equal(entitiesB.get("entityType"), EntityB.class.getName()));
final ListJoin<TestEntity, EntityB> joinEntityB = cb.treat(entitiesB, EntityB.class);
selections.add(joinEntityB.get("id"));
selections.add(joinEntityB.get("valueB"));
query.multiselect(selections);
final List<Tuple> resultList = em.createQuery(query).getResultList();
assertThat(resultList.size(), is(10));
} finally {
em.close();
}
}
use of javax.persistence.Tuple in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testVariousTupleAccessMethods.
@Test
public void testVariousTupleAccessMethods() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
Customer c1 = new Customer();
c1.setId("c1");
c1.setAge(18);
c1.setName("Bob");
em.persist(c1);
em.getTransaction().commit();
em.close();
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
final CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Customer> customerRoot = criteria.from(Customer.class);
Path<String> namePath = customerRoot.get(Customer_.name);
namePath.alias("NAME");
Path<Integer> agePath = customerRoot.get(Customer_.age);
agePath.alias("AGE");
criteria.multiselect(namePath, agePath);
List<Tuple> results = em.createQuery(criteria).getResultList();
Tuple tuple = results.get(0);
assertNotNull(tuple);
assertNotNull(tuple.get("NAME"));
assertNotNull(tuple.get("NAME", String.class));
try {
tuple.get("NAME", Date.class);
fail("Accessing Customer#name tuple as Date should have thrown exception");
} catch (IllegalArgumentException expected) {
}
em.getTransaction().commit();
em.close();
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
em.createQuery("delete Customer").executeUpdate();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.Tuple in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testIllegalArgumentExceptionBuildingTupleWithSameAliases.
@Test
public void testIllegalArgumentExceptionBuildingTupleWithSameAliases() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
final CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Customer> customerRoot = criteria.from(Customer.class);
Path<String> namePath = customerRoot.get(Customer_.name);
namePath.alias("age");
Path<Integer> agePath = customerRoot.get(Customer_.age);
agePath.alias("age");
try {
criteria.multiselect(namePath, agePath);
fail("Attempt to define multi-select with same aliases should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
em.getTransaction().commit();
em.close();
}
use of javax.persistence.Tuple in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testTuple.
@Test
public void testTuple() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
Customer c1 = new Customer();
c1.setId("c1");
c1.setAge(18);
c1.setName("Bob");
em.persist(c1);
em.getTransaction().commit();
em.close();
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
final CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Customer> customerRoot = criteria.from(Customer.class);
Path<String> namePath = customerRoot.get(Customer_.name);
Path<Integer> agePath = customerRoot.get(Customer_.age);
agePath.alias("age");
criteria.multiselect(namePath, agePath);
List<Tuple> results = em.createQuery(criteria).getResultList();
assertEquals(1, results.size());
Object resultElement = results.get(0);
assertTrue("Check result 'row' as Tuple", Tuple.class.isInstance(resultElement));
Tuple resultElementTuple = (Tuple) resultElement;
Object[] tupleArray = resultElementTuple.toArray();
assertEquals(2, tupleArray.length);
assertEquals(tupleArray[0], resultElementTuple.get(0));
assertEquals(resultElementTuple.get(namePath), resultElementTuple.get(0));
assertEquals(tupleArray[1], resultElementTuple.get(1));
assertEquals(resultElementTuple.get(agePath), resultElementTuple.get(1));
assertEquals(resultElementTuple.get(agePath), resultElementTuple.get("age"));
em.getTransaction().commit();
em.close();
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
em.createQuery("delete Customer").executeUpdate();
em.getTransaction().commit();
em.close();
}
Aggregations