Search in sources :

Example 91 with EntityManager

use of javax.persistence.EntityManager 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 92 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class CorrelatedSubqueryTest method testBasicCorrelation.

@Test
public void testBasicCorrelation() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
    Root<Customer> customer = criteria.from(Customer.class);
    criteria.select(customer);
    Subquery<Order> orderSubquery = criteria.subquery(Order.class);
    Root<Customer> customerCorrelationRoot = orderSubquery.correlate(customer);
    Join<Customer, Order> customerOrderCorrelationJoin = customerCorrelationRoot.join(Customer_.orders);
    orderSubquery.select(customerOrderCorrelationJoin);
    criteria.where(builder.not(builder.exists(orderSubquery)));
    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 93 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class CorrelatedSubqueryTest method testRestrictedCorrelation.

@Test
public void testRestrictedCorrelation() {
    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(Order_.customer);
    customerSubquery.where(builder.like(orderCustomerJoin.get(Customer_.name), "%Caruso")).select(orderCustomerJoin);
    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 94 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class CorrelatedSubqueryTest method testCorrelationExplicitSelectionCorrelation.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-3032")
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(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) LineItem(org.hibernate.jpa.test.metamodel.LineItem) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 95 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class UncorrelatedSubqueryTest method testGetCorrelatedParentIllegalStateException.

@Test
public void testGetCorrelatedParentIllegalStateException() {
    // test that attempting to call getCorrelatedParent on a uncorrelated query/subquery
    // throws ISE
    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)));
    assertFalse(customerRoot.isCorrelated());
    assertFalse(subqueryOrderRoot.isCorrelated());
    try {
        customerRoot.getCorrelationParent();
        fail("Should have resulted in IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        subqueryOrderRoot.getCorrelationParent();
        fail("Should have resulted in IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    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

EntityManager (javax.persistence.EntityManager)1435 Test (org.junit.Test)879 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)211 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)75 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)59 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 PersistenceException (javax.persistence.PersistenceException)43 ArrayList (java.util.ArrayList)41 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)38 Item (org.hibernate.jpa.test.Item)36 Session (org.hibernate.Session)35