use of javax.persistence.criteria.CriteriaBuilder 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.criteria.CriteriaBuilder 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.criteria.CriteriaBuilder 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();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaLiteralInSelectExpressionTest method testNullLiteral.
@Test
@TestForIssue(jiraKey = "HHH-10861")
public void testNullLiteral() throws Exception {
final EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery(MyEntityDTO.class);
final Root<MyEntity> entity = query.from(MyEntity.class);
query.multiselect(criteriaBuilder.literal(false), criteriaBuilder.nullLiteral(String.class));
final List<MyEntityDTO> dtos = entityManager.createQuery(query).getResultList();
assertThat(dtos.size(), is(1));
assertThat(dtos.get(0).active, is(false));
assertThat(dtos.get(0).name, nullValue());
assertThat(dtos.get(0).surname, nullValue());
entityManager.getTransaction().commit();
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
}
throw e;
} finally {
entityManager.close();
}
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaLiteralsTest method testLiteralsInSelectClause.
@Test
public void testLiteralsInSelectClause() throws Exception {
try {
doInJPA(this::entityManagerFactory, entityManager -> {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<String> query = cb.createQuery(String.class);
Root<Book> root = query.from(Book.class);
ListJoin<Book, Author> authors = root.joinList("authors");
query.where(cb.equal(root.get("name"), "Java Persistence with Hibernate")).select(cb.literal("( SELECT REPEAT('abc' || ' ', 10000000000 FROM MY_ENTITY )"));
entityManager.createQuery(query).getResultList();
});
} catch (Exception expected) {
assertEquals(QuerySyntaxException.class, expected.getCause().getClass());
}
}
Aggregations