Search in sources :

Example 1 with Customer

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

the class TupleCriteriaTest method testIllegalArgumentExceptionBuildingTupleWithSameAliases.

@Test
public void testIllegalArgumentExceptionBuildingTupleWithSameAliases() {
    EntityManager em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    final CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
    Root<Customer> customerRoot = criteria.from(Customer.class);
    Path<String> namePath = customerRoot.get(Customer_.name);
    namePath.alias("age");
    Path<Integer> agePath = customerRoot.get(Customer_.age);
    agePath.alias("age");
    try {
        criteria.multiselect(namePath, agePath);
        fail("Attempt to define multi-select with same aliases should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) Tuple(jakarta.persistence.Tuple) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 2 with Customer

use of org.hibernate.orm.test.jpa.metamodel.Customer 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 3 with Customer

use of org.hibernate.orm.test.jpa.metamodel.Customer 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 4 with Customer

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

the class TupleCriteriaTest method testArray.

@Test
public void testArray() {
    EntityManager em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    Customer c1 = new Customer();
    c1.setId("c1");
    c1.setAge(18);
    c1.setName("Bob");
    em.persist(c1);
    em.getTransaction().commit();
    em.close();
    em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> q = cb.createQuery(Object[].class);
    Root<Customer> c = q.from(Customer.class);
    q.select(cb.array(c.get(Customer_.name), c.get(Customer_.age)));
    List<Object[]> result = em.createQuery(q).getResultList();
    assertEquals(1, result.size());
    assertEquals(c1.getName(), result.get(0)[0]);
    assertEquals(c1.getAge(), result.get(0)[1]);
    em.getTransaction().commit();
    em.close();
    em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    em.createQuery("delete Customer").executeUpdate();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) 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 5 with Customer

use of org.hibernate.orm.test.jpa.metamodel.Customer 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

Customer (org.hibernate.orm.test.jpa.metamodel.Customer)25 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)24 EntityManager (jakarta.persistence.EntityManager)21 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)19 Test (org.junit.jupiter.api.Test)19 Order (org.hibernate.orm.test.jpa.metamodel.Order)7 Tuple (jakarta.persistence.Tuple)5 Test (org.junit.Test)5 TestForIssue (org.hibernate.testing.TestForIssue)4 CriteriaDelete (jakarta.persistence.criteria.CriteriaDelete)2 CriteriaUpdate (jakarta.persistence.criteria.CriteriaUpdate)2 Root (jakarta.persistence.criteria.Root)2 SkipForDialect (org.hibernate.testing.orm.junit.SkipForDialect)2 Query (jakarta.persistence.Query)1 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 Join (jakarta.persistence.criteria.Join)1 LineItem (org.hibernate.orm.test.jpa.metamodel.LineItem)1 JiraKey (org.hibernate.testing.orm.junit.JiraKey)1 Before (org.junit.Before)1