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();
}
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();
}
}
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());
}
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();
}
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();
}
Aggregations