Search in sources :

Example 6 with SkipForDialect

use of org.hibernate.testing.SkipForDialect 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 7 with SkipForDialect

use of org.hibernate.testing.SkipForDialect 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 8 with SkipForDialect

use of org.hibernate.testing.SkipForDialect 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 9 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class LockTest method testContendedPessimisticLock.

@Test
@SkipForDialect(HSQLDialect.class)
// only.
@SkipForDialect(value = { SybaseASE15Dialect.class }, strictMatching = true, jiraKey = "HHH-6820")
public void testContendedPessimisticLock() throws Exception {
    final EntityManager em = getOrCreateEntityManager();
    final EntityManager isolatedEntityManager = createIsolatedEntityManager();
    Lock lock = createAndPersistLockInstance(em);
    try {
        inFirstTransactionReloadAndModifyLockInstance(em, lock);
        final CountDownLatch latch = new CountDownLatch(1);
        FutureTask<Boolean> future = inBackgroundThreadStartSecondTransactionAndReadLockInstance(latch, isolatedEntityManager);
        // wait with timeout on the background thread
        log.debug("testContendedPessimisticLock:  wait on BG thread");
        boolean backGroundThreadCompleted = latch.await(3, TimeUnit.SECONDS);
        if (backGroundThreadCompleted) {
            // the background thread read a value. At the very least we need to assert that he did not see the
            // changed value
            boolean backgroundThreadHasReadNewValue = future.get();
            assertFalse("The background thread is not allowed to see the updated value while the first transaction has not committed yet", backgroundThreadHasReadNewValue);
            em.getTransaction().commit();
        } else {
            log.debug("The background thread was blocked");
            // commit first transaction so that background thread can continue
            em.getTransaction().commit();
            boolean backgroundThreadHasReadNewValue = future.get();
            assertTrue("Background thread should read the new value afterQuery being unblocked", backgroundThreadHasReadNewValue);
        }
    } finally {
        cleanup(em, isolatedEntityManager, lock);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) CountDownLatch(java.util.concurrent.CountDownLatch) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 10 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class QueryTest method testNullNamedParameterParameterIncompatible.

@Test
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNullNamedParameterParameterIncompatible() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Item item = new Item("Mouse", "Micro$oft mouse");
        em.persist(item);
        Query q = em.createQuery("from Item i where i.intVal=:iVal");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return "iVal";
            }

            @Override
            public Integer getPosition() {
                return null;
            }

            @Override
            public Class getParameterType() {
                return Long.class;
            }
        };
        q.setParameter(p, null);
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createQuery("from Item i where i.intVal is null and :iVal is null");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createQuery("from Item i where i.intVal is null or i.intVal = :iVal");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Parameter(javax.persistence.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Aggregations

SkipForDialect (org.hibernate.testing.SkipForDialect)87 Test (org.junit.Test)86 Session (org.hibernate.Session)66 Transaction (org.hibernate.Transaction)36 List (java.util.List)26 ArrayList (java.util.ArrayList)20 TestForIssue (org.hibernate.testing.TestForIssue)16 EntityManager (javax.persistence.EntityManager)15 Query (javax.persistence.Query)11 Item (org.hibernate.jpa.test.Item)10 Query (org.hibernate.Query)7 Parameter (javax.persistence.Parameter)6 BigDecimal (java.math.BigDecimal)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 SQLQuery (org.hibernate.SQLQuery)4 ScrollableResults (org.hibernate.ScrollableResults)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Date (java.util.Date)3 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)3