Search in sources :

Example 26 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 27 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 28 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 29 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 30 with CriteriaBuilder

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) Customer(org.hibernate.jpa.test.metamodel.Customer) Join(javax.persistence.criteria.Join) CollectionJoin(javax.persistence.criteria.CollectionJoin) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2