Search in sources :

Example 41 with TestForIssue

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

the class EnumIsMemberTest method testQueryEnumCollection.

@Test
@TestForIssue(jiraKey = "HHH-9605")
public void testQueryEnumCollection() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    User user = new User();
    user.setId(1l);
    user.getRoles().add(User.Role.Admin);
    em.persist(user);
    em.getTransaction().commit();
    em.getTransaction().begin();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Expression<Set<User.Role>> roles = root.get(User_.roles);
    // Using the correct collection of enums and an enum parameter
    query.where(builder.isMember(User.Role.Admin, roles));
    TypedQuery<User> typedQuery = em.createQuery(query);
    List<User> users = typedQuery.getResultList();
    assertEquals(1, users.size());
    em.getTransaction().commit();
    em.getTransaction().begin();
    // delete
    em.remove(user);
    em.getTransaction().commit();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Set(java.util.Set) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 42 with TestForIssue

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

the class CriteriaLiteralInSelectExpressionTest method testBooleanLiteral.

@Test
@TestForIssue(jiraKey = "HHH-10729")
public void testBooleanLiteral() throws Exception {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        entityManager.getTransaction().begin();
        final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery(MyEntityDTO.class);
        final Root<MyEntity> entity = query.from(MyEntity.class);
        query.multiselect(criteriaBuilder.literal(false), entity.get("name"));
        final List<MyEntityDTO> dtos = entityManager.createQuery(query).getResultList();
        assertThat(dtos.size(), is(1));
        assertThat(dtos.get(0).active, is(false));
        assertThat(dtos.get(0).name, is("Fab"));
        assertThat(dtos.get(0).surname, nullValue());
        entityManager.getTransaction().commit();
    } catch (Exception e) {
        if (entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().rollback();
        }
        throw e;
    } finally {
        entityManager.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 43 with TestForIssue

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

the class ListIndexTest method testListIndex.

@Test
@TestForIssue(jiraKey = "HHH-8404")
public void testListIndex() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Address address1 = new Address();
    address1.setId("a1");
    Phone phone1 = new Phone();
    phone1.setId("p1");
    phone1.setAddress(address1);
    Phone phone2 = new Phone();
    phone2.setId("p2");
    phone2.setAddress(address1);
    address1.getPhones().add(phone1);
    address1.getPhones().add(phone2);
    Address address2 = new Address();
    address2.setId("a2");
    Phone phone3 = new Phone();
    phone3.setId("p3");
    phone3.setAddress(address2);
    address2.getPhones().add(phone3);
    em.persist(phone1);
    em.persist(phone2);
    em.persist(phone3);
    em.persist(address1);
    em.persist(address2);
    em.getTransaction().commit();
    em.clear();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Address> criteria = cb.createQuery(Address.class);
    Root<Address> addressRoot = criteria.from(Address.class);
    ListJoin<Address, Phone> phones = addressRoot.join(Address_.phones);
    criteria.where(cb.gt(phones.index(), 0));
    List<Address> results = em.createQuery(criteria).getResultList();
    assertNotNull(results);
    // Ensure that the "index(phones) > 0" condition was included on the inner join, meaning only address1
    // (> 1 phone) was returned.
    assertEquals(1, results.size());
    assertEquals(address1.getId(), results.get(0).getId());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Address(org.hibernate.jpa.test.metamodel.Address) Phone(org.hibernate.jpa.test.metamodel.Phone) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 44 with TestForIssue

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

the class ExceptionTest method testInterceptor.

@Test
@TestForIssue(jiraKey = "HHH-4676")
public void testInterceptor() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Instrument instrument = new Instrument();
    instrument.setName("Guitar");
    try {
        em.persist(instrument);
        fail("Commit should have failed.");
    } catch (RuntimeException e) {
        assertTrue(em.getTransaction().getRollbackOnly());
    }
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 45 with TestForIssue

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

the class LockTest method testFindWithPessimisticWriteLockTimeoutException.

//5 minutes
@Test(timeout = 5 * 60 * 1000)
@TestForIssue(jiraKey = "HHH-7252")
@RequiresDialectFeature(value = DialectChecks.SupportsLockTimeouts.class, comment = "Test verifies proper exception throwing when a lock timeout is specified.", jiraKey = "HHH-7252")
public void testFindWithPessimisticWriteLockTimeoutException() {
    Lock lock = new Lock();
    lock.setName("name");
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(lock);
    em.getTransaction().commit();
    em.close();
    EntityManager em2 = createIsolatedEntityManager();
    em2.getTransaction().begin();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(AvailableSettings.LOCK_TIMEOUT, 0L);
    Lock lock2 = em2.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
    assertEquals("lock mode should be PESSIMISTIC_WRITE ", LockModeType.PESSIMISTIC_WRITE, em2.getLockMode(lock2));
    EntityManager em3 = createIsolatedEntityManager();
    em3.getTransaction().begin();
    try {
        em3.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
        fail("Exception should be thrown");
    } catch (LockTimeoutException lte) {
    // Proper exception thrown for dialect supporting lock timeouts when an immediate timeout is set.
    } catch (PessimisticLockException pe) {
        fail("Find with immediate timeout should have thrown LockTimeoutException.");
    } catch (PersistenceException pe) {
        log.info("EntityManager.find() for PESSIMISTIC_WRITE with timeout of 0 threw a PersistenceException.\n" + "This is likely a consequence of " + getDialect().getClass().getName() + " not properly mapping SQL errors into the correct HibernateException subtypes.\n" + "See HHH-7251 for an example of one such situation.", pe);
        fail("EntityManager should be throwing LockTimeoutException.");
    } finally {
        if (em3.getTransaction().getRollbackOnly()) {
            em3.getTransaction().rollback();
        } else {
            em3.getTransaction().commit();
        }
        em3.close();
    }
    em2.getTransaction().commit();
    em2.getTransaction().begin();
    em2.remove(lock2);
    em2.getTransaction().commit();
    em2.close();
}
Also used : EntityManager(javax.persistence.EntityManager) HashMap(java.util.HashMap) PersistenceException(javax.persistence.PersistenceException) LockTimeoutException(javax.persistence.LockTimeoutException) PessimisticLockException(javax.persistence.PessimisticLockException) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TestForIssue (org.hibernate.testing.TestForIssue)649 Test (org.junit.Test)647 Session (org.hibernate.Session)357 EntityManager (javax.persistence.EntityManager)97 List (java.util.List)91 Transaction (org.hibernate.Transaction)88 MetadataSources (org.hibernate.boot.MetadataSources)47 ArrayList (java.util.ArrayList)38 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)38 Query (org.hibernate.Query)28 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)25 Metadata (org.hibernate.boot.Metadata)24 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)24 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Map (java.util.Map)22 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)19 HashMap (java.util.HashMap)18 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)18 PersistentClass (org.hibernate.mapping.PersistentClass)18 HibernateException (org.hibernate.HibernateException)16