Search in sources :

Example 1 with Customer

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

the class QueryBuilderTest method testMultiselectWithPredicates.

@Test
@TestForIssue(jiraKey = "HHH-8699")
// For now, restrict to H2.  Selecting w/ predicate functions cause issues for too many dialects.
@RequiresDialect(value = H2Dialect.class, jiraKey = "HHH-9092")
public void testMultiselectWithPredicates() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    CriteriaQuery<Customer> cq = cb.createQuery(Customer.class);
    Root<Customer> r = cq.from(Customer.class);
    cq.multiselect(r.get(Customer_.id), r.get(Customer_.name), cb.concat("Hello ", r.get(Customer_.name)), cb.isNotNull(r.get(Customer_.age)));
    TypedQuery<Customer> tq = em.createQuery(cq);
    tq.getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 2 with Customer

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

Example 3 with Customer

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

Example 4 with Customer

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

Example 5 with Customer

use of org.hibernate.jpa.test.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.<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)

Aggregations

Customer (org.hibernate.jpa.test.metamodel.Customer)25 Test (org.junit.Test)24 EntityManager (javax.persistence.EntityManager)21 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)21 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)19 Order (org.hibernate.jpa.test.metamodel.Order)7 Tuple (javax.persistence.Tuple)4 TestForIssue (org.hibernate.testing.TestForIssue)4 CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)3 CriteriaDelete (javax.persistence.criteria.CriteriaDelete)2 CriteriaUpdate (javax.persistence.criteria.CriteriaUpdate)2 Root (javax.persistence.criteria.Root)2 SkipForDialect (org.hibernate.testing.SkipForDialect)2 Query (javax.persistence.Query)1 CollectionJoin (javax.persistence.criteria.CollectionJoin)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Join (javax.persistence.criteria.Join)1 LineItem (org.hibernate.jpa.test.metamodel.LineItem)1 MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)1 RequiresDialect (org.hibernate.testing.RequiresDialect)1