use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaCompilingTest method testTrimAChar.
@Test
@TestForIssue(jiraKey = "HHH-11393")
public void testTrimAChar() {
TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Customer> query = criteriaBuilder.createQuery(Customer.class);
final Root<Customer> from = query.from(Customer.class);
query.select(from);
query.where(criteriaBuilder.equal(criteriaBuilder.trim(CriteriaBuilder.Trimspec.LEADING, criteriaBuilder.literal('R'), from.get("name")), " Vincent"));
List<Customer> resultList = entityManager.createQuery(query).getResultList();
assertThat(resultList.size(), is(1));
});
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class ManipulationCriteriaTest method testJoinsAndFetchesDisallowed.
@Test
public void testJoinsAndFetchesDisallowed() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
{
try {
CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
Root<Customer> root = deleteCriteria.from(Customer.class);
root.join(Customer_.spouse);
em.createQuery(deleteCriteria).executeUpdate();
fail("Expected failure dues to attempt to join");
} catch (IllegalArgumentException expected) {
}
}
{
try {
CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
Root<Customer> root = deleteCriteria.from(Customer.class);
root.fetch(Customer_.spouse);
em.createQuery(deleteCriteria).executeUpdate();
fail("Expected failure dues to attempt to fetch");
} catch (IllegalArgumentException expected) {
}
}
{
try {
CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
Root<Customer> root = updateCriteria.from(Customer.class);
root.join(Customer_.spouse);
em.createQuery(updateCriteria).executeUpdate();
fail("Expected failure dues to attempt to join");
} catch (IllegalArgumentException expected) {
}
}
{
try {
CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
Root<Customer> root = updateCriteria.from(Customer.class);
root.fetch(Customer_.spouse);
em.createQuery(updateCriteria).executeUpdate();
fail("Expected failure dues to attempt to fetch");
} catch (IllegalArgumentException expected) {
}
}
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class ManipulationCriteriaTest method testDeleteWithUnCorrelatedSubquery.
@Test
// MySQL does not allow "delete/update from" and subqueries to use the same table
@SkipForDialect(MySQLDialect.class)
public void testDeleteWithUnCorrelatedSubquery() {
CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
// attempt to delete Customers who's age is less than the AVG age
CriteriaDelete<Customer> criteria = builder.createCriteriaDelete(Customer.class);
Root<Customer> customerRoot = criteria.from(Customer.class);
Subquery<Double> subCriteria = criteria.subquery(Double.class);
Root<Customer> subQueryCustomerRoot = subCriteria.from(Customer.class);
subCriteria.select(builder.avg(subQueryCustomerRoot.get(Customer_.age)));
// also illustrates the new capability to use the subquery selection as an expression!
criteria.where(builder.lessThan(customerRoot.get(Customer_.age), subCriteria.getSelection().as(Integer.class)));
// make sure Subquery#getParent fails...
try {
subCriteria.getParent();
fail("Expecting Subquery.getParent call to fail on DELETE containing criteria");
} catch (IllegalStateException expected) {
}
Query query = em.createQuery(criteria);
try {
// first, make sure an attempt to list fails
query.getResultList();
fail("Attempt to getResultList() on delete criteria should have failed");
} catch (IllegalStateException expected) {
}
query.executeUpdate();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class ParameterTest method testParameterInParameterList2.
@Test
@TestForIssue(jiraKey = "HHH-10870")
public void testParameterInParameterList2() {
TransactionUtil.doInJPA(this::entityManagerFactory, em -> {
final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
final CriteriaQuery<MultiTypedBasicAttributesEntity> criteria = criteriaBuilder.createQuery(MultiTypedBasicAttributesEntity.class);
final ParameterExpression<Iterable> parameter = criteriaBuilder.parameter(Iterable.class);
final Root<MultiTypedBasicAttributesEntity> root = criteria.from(MultiTypedBasicAttributesEntity.class);
criteria.select(root).where(root.get("id").in(parameter));
final TypedQuery<MultiTypedBasicAttributesEntity> query1 = em.createQuery(criteria);
query1.setParameter(parameter, Arrays.asList(1L, 2L, 3L));
query1.getResultList();
});
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class TestConnectionPool method testConnectionPoolDoesNotConsumeAllConnections.
@Test
public void testConnectionPoolDoesNotConsumeAllConnections() {
for (int i = 0; i < CONNECTION_POOL_SIZE + 1; ++i) {
EntityManager entityManager = getOrCreateEntityManager();
try {
for (int j = 0; j < 2; j++) {
try {
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<TestEntity> criteriaQuery = builder.createQuery(TestEntity.class);
criteriaQuery.select(criteriaQuery.from(TestEntity.class));
entityManager.createQuery(criteriaQuery).getResultList();
} catch (PersistenceException e) {
if (e.getCause() instanceof SQLGrammarException) {
//expected, the schema was not created
} else {
throw e;
}
}
}
} finally {
entityManager.close();
}
}
}
Aggregations