use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaLiteralsTest method testLiteralsInWhereClause.
@Test
public void testLiteralsInWhereClause() throws Exception {
doInJPA(this::entityManagerFactory, entityManager -> {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = cb.createQuery(Tuple.class);
final Root<Book> entity = query.from(Book.class);
query.where(cb.equal(entity.get("name"), cb.literal("( SELECT REPEAT('abc' || ' ', 10000000000 FROM MY_ENTITY )")));
query.multiselect(cb.literal("abc"), entity.get("name"));
connectionProvider.clear();
List<Tuple> tuples = entityManager.createQuery(query).getResultList();
assertEquals(1, connectionProvider.getPreparedStatements().size());
assertNotNull(connectionProvider.getPreparedStatement("select 'abc' as col_0_0_, criteriali0_.name as col_1_0_ from Book criteriali0_ where criteriali0_.name=?"));
assertTrue(tuples.isEmpty());
});
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaLiteralsTest method testCriteriaParameters.
@Test
public void testCriteriaParameters() throws Exception {
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"), "( SELECT REPEAT('abc' || ' ', 10000000000 FROM MY_ENTITY )"), cb.equal(authors.index(), 0)).select(authors.get("name"));
connectionProvider.clear();
entityManager.createQuery(query).getResultList();
assertEquals(1, connectionProvider.getPreparedStatements().size());
assertNotNull(connectionProvider.getPreparedStatement("select authors1_.name as col_0_0_ from Book criteriali0_ inner join Author authors1_ on criteriali0_.id=authors1_.book_id where criteriali0_.name=? and authors1_.index_id=0"));
});
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class NullLiteralExpression method testNullLiteralExpressionInCriteriaUpdate.
@Test
@TestForIssue(jiraKey = "HHH-11159")
public void testNullLiteralExpressionInCriteriaUpdate() {
EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaUpdate<Person> criteriaUpdate = builder.createCriteriaUpdate(Person.class);
criteriaUpdate.from(Person.class);
criteriaUpdate.set(Person_.subject, builder.nullLiteral(Subject.class));
entityManager.createQuery(criteriaUpdate).executeUpdate();
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 AbstractPathImplTest method testIllegalDereference.
@Test
public void testIllegalDereference() {
EntityManager em = getOrCreateEntityManager();
try {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
Root<Order> orderRoot = criteria.from(Order.class);
Path simplePath = orderRoot.get("totalPrice");
// this should cause an ISE...
try {
simplePath.get("yabbadabbado");
fail("Attempt to dereference basic path should throw IllegalStateException");
} catch (IllegalStateException expected) {
}
} finally {
em.close();
}
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class AbstractPathImplTest method testGetNonExistingAttributeViaName.
@Test(expected = IllegalArgumentException.class)
public void testGetNonExistingAttributeViaName() {
EntityManager em = getOrCreateEntityManager();
try {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
Root<Order> orderRoot = criteria.from(Order.class);
orderRoot.get("nonExistingAttribute");
} finally {
em.close();
}
}
Aggregations