Search in sources :

Example 6 with Order

use of org.hibernate.orm.test.jpa.metamodel.Order in project hibernate-orm by hibernate.

the class PredicateTest method testQuotientConversion.

@Test
@TestForIssue(jiraKey = "HHH-5803")
@SkipForDialect(dialectClass = CockroachDialect.class, reason = "https://github.com/cockroachdb/cockroach/issues/41943")
public void testQuotientConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    Long longValue = 999999999L;
    Path<Double> doublePath = orderRoot.get(Order_.totalPrice);
    Path<Integer> integerPath = orderRoot.get(Order_.customer).get(Customer_.age);
    orderCriteria.select(orderRoot);
    Predicate p = builder.ge(builder.quot(integerPath, doublePath), longValue);
    orderCriteria.where(p);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertTrue(orders.size() == 0);
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Predicate(jakarta.persistence.criteria.Predicate) SkipForDialect(org.hibernate.testing.orm.junit.SkipForDialect) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 7 with Order

use of org.hibernate.orm.test.jpa.metamodel.Order in project hibernate-orm by hibernate.

the class PredicateTest method testByteArray.

/**
 * Check predicate for field which has simple byte array type (byte[]).
 */
@Test
@JiraKey("HHH-10603")
@SkipForDialect(dialectClass = OracleDialect.class, majorVersion = 12, reason = "Oracle12cDialect uses blob to store byte arrays and it's not possible to compare blobs with simple equality operators.")
public void testByteArray() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    orderCriteria.select(orderRoot);
    Predicate p = builder.equal(orderRoot.get("number"), new byte[] { '1', '2' });
    orderCriteria.where(p);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertTrue(orders.size() == 0);
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Predicate(jakarta.persistence.criteria.Predicate) SkipForDialect(org.hibernate.testing.orm.junit.SkipForDialect) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Example 8 with Order

use of org.hibernate.orm.test.jpa.metamodel.Order 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.get("name"), "%Caruso"));
    criteria.where(builder.exists(customerSubquery));
    em.createQuery(criteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 9 with Order

use of org.hibernate.orm.test.jpa.metamodel.Order in project hibernate-orm by hibernate.

the class CorrelatedSubqueryTest method testCorrelationExplicitSelectionCorrelation.

@Test
@JiraKey("HHH-3032")
@SkipForDialect(dialectClass = SybaseASEDialect.class, majorVersion = 15)
public void testCorrelationExplicitSelectionCorrelation() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Customer> customerCriteria = builder.createQuery(Customer.class);
    Root<Customer> customer = customerCriteria.from(Customer.class);
    Join<Customer, Order> o = customer.join(Customer_.orders);
    Subquery<Order> sq = customerCriteria.subquery(Order.class);
    Join<Customer, Order> sqo = sq.correlate(o);
    Join<Order, LineItem> sql = sqo.join(Order_.lineItems);
    sq.where(builder.gt(sql.get(LineItem_.quantity), 3));
    // use the correlation itself as the subquery selection (initially caused problems wrt aliases)
    sq.select(sqo);
    customerCriteria.select(customer).distinct(true);
    customerCriteria.where(builder.exists(sq));
    em.createQuery(customerCriteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) LineItem(org.hibernate.orm.test.jpa.metamodel.LineItem) SkipForDialect(org.hibernate.testing.orm.junit.SkipForDialect) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Example 10 with Order

use of org.hibernate.orm.test.jpa.metamodel.Order in project hibernate-orm by hibernate.

the class UncorrelatedSubqueryTest method testEqualAll.

@Test
public void testEqualAll() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
    Root<Customer> customerRoot = criteria.from(Customer.class);
    Join<Customer, Order> orderJoin = customerRoot.join(Customer_.orders);
    criteria.select(customerRoot);
    Subquery<Double> subCriteria = criteria.subquery(Double.class);
    Root<Order> subqueryOrderRoot = subCriteria.from(Order.class);
    subCriteria.select(builder.min(subqueryOrderRoot.get(Order_.totalPrice)));
    criteria.where(builder.equal(orderJoin.get("totalPrice"), builder.all(subCriteria)));
    em.createQuery(criteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Aggregations

Order (org.hibernate.orm.test.jpa.metamodel.Order)23 EntityManager (jakarta.persistence.EntityManager)21 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)21 Test (org.junit.jupiter.api.Test)21 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)10 Predicate (jakarta.persistence.criteria.Predicate)8 Customer (org.hibernate.orm.test.jpa.metamodel.Customer)7 TestForIssue (org.hibernate.testing.TestForIssue)4 SkipForDialect (org.hibernate.testing.orm.junit.SkipForDialect)3 JiraKey (org.hibernate.testing.orm.junit.JiraKey)2 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 Join (jakarta.persistence.criteria.Join)1 Path (jakarta.persistence.criteria.Path)1 Root (jakarta.persistence.criteria.Root)1 ScrollableResults (org.hibernate.ScrollableResults)1 Session (org.hibernate.Session)1 CreditCard (org.hibernate.orm.test.jpa.metamodel.CreditCard)1 LineItem (org.hibernate.orm.test.jpa.metamodel.LineItem)1 ExpectedException (org.hibernate.testing.orm.junit.ExpectedException)1 Test (org.junit.Test)1