use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class QueryTest method testCriteriaBetween.
@Test
@TestForIssue(jiraKey = "HHH-9356")
public void testCriteriaBetween() {
final EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
final Root<Employee> root = query.from(Employee.class);
query.select(root);
query.where(cb.between(root.<Float>get("salary"), new Float(300f), new Float(400f)));
final List<Employee> result0 = em.createQuery(query).getResultList();
assertEquals(0, result0.size());
query.where(cb.between(root.<Float>get("salary"), new Float(100f), new Float(200f)));
final List<Employee> result1 = em.createQuery(query).getResultList();
assertEquals(0, result1.size());
query.where(cb.between(root.<Float>get("salary"), new Float(200f), new Float(300f)));
final List<Employee> result2 = em.createQuery(query).getResultList();
assertEquals(1, result2.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class BasicCriteriaUsageTest method testFunctionCriteria.
@Test
@TestForIssue(jiraKey = "HHH-8373")
public void testFunctionCriteria() {
Wall wall = new Wall();
wall.setColor("yellow");
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(wall);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Wall> query = cb.createQuery(Wall.class);
Root<Wall> root = query.from(Wall.class);
query.select(root).where(cb.equal(root.get("color"), cb.lower(cb.literal("YELLOW"))));
Wall resultItem = em.createQuery(query).getSingleResult();
assertNotNull(resultItem);
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CastTest method testCastToString.
@Test
@SkipForDialect(value = DerbyDialect.class, comment = "Derby does not support cast from INTEGER to VARCHAR")
@TestForIssue(jiraKey = "HHH-5755")
public void testCastToString() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Product product = new Product();
product.setId("product1");
product.setPrice(1.23d);
product.setQuantity(QUANTITY);
product.setPartNumber(((long) Integer.MAX_VALUE) + 1);
product.setRating(1.999f);
product.setSomeBigInteger(BigInteger.valueOf(987654321));
product.setSomeBigDecimal(BigDecimal.valueOf(987654.321));
em.persist(product);
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
Root<Product> root = criteria.from(Product.class);
criteria.where(builder.equal(root.get(Product_.quantity).as(String.class), builder.literal(String.valueOf(QUANTITY))));
List<Product> result = em.createQuery(criteria).getResultList();
Assert.assertEquals(1, result.size());
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.createQuery("delete Product").executeUpdate();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CorrelatedSubqueryTest method testRestrictedCorrelationNoExplicitSelection.
@Test
public void testRestrictedCorrelationNoExplicitSelection() {
CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
Root<Order> orderRoot = criteria.from(Order.class);
criteria.select(orderRoot);
// create correlated subquery
Subquery<Customer> customerSubquery = criteria.subquery(Customer.class);
Root<Order> orderRootCorrelation = customerSubquery.correlate(orderRoot);
Join<Order, Customer> orderCustomerJoin = orderRootCorrelation.join("customer");
customerSubquery.where(builder.like(orderCustomerJoin.<String>get("name"), "%Caruso"));
criteria.where(builder.exists(customerSubquery));
em.createQuery(criteria).getResultList();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CorrelatedSubqueryTest method testCorrelatedJoinsFromSubquery.
@Test
@TestForIssue(jiraKey = "HHH-8556")
public void testCorrelatedJoinsFromSubquery() {
CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
CriteriaQuery<Customer> cquery = builder.createQuery(Customer.class);
Root<Customer> customer = cquery.from(Customer.class);
cquery.select(customer);
Subquery<Order> sq = cquery.subquery(Order.class);
Join<Customer, Order> sqo = sq.correlate(customer.join(Customer_.orders));
sq.select(sqo);
Set<Join<?, ?>> cJoins = sq.getCorrelatedJoins();
// ensure the join is returned in #getCorrelatedJoins
assertEquals(cJoins.size(), 1);
}
Aggregations