use of org.hibernate.query.NativeQuery in project dropwizard by dropwizard.
the class SessionFactoryHealthCheckTest method isHealthyIfNoExceptionIsThrown.
@Test
public void isHealthyIfNoExceptionIsThrown() throws Exception {
final Session session = mock(Session.class);
when(factory.openSession()).thenReturn(session);
final Transaction transaction = mock(Transaction.class);
when(session.beginTransaction()).thenReturn(transaction);
final NativeQuery query = mock(NativeQuery.class);
when(session.createNativeQuery(anyString())).thenReturn(query);
assertThat(healthCheck.execute().isHealthy()).isTrue();
final InOrder inOrder = inOrder(factory, session, transaction, query);
inOrder.verify(factory).openSession();
inOrder.verify(session).beginTransaction();
inOrder.verify(session).createNativeQuery("SELECT 1");
inOrder.verify(query).list();
inOrder.verify(transaction).commit();
inOrder.verify(session).close();
}
use of org.hibernate.query.NativeQuery in project hibernate-orm by hibernate.
the class QueryLockingTest method testNativeSql.
@Test
public void testNativeSql() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
NativeQuery query = em.createNativeQuery("select * from lockable l").unwrap(NativeQuery.class);
// the spec disallows calling setLockMode in a native SQL query
try {
query.setLockMode(LockModeType.READ);
fail("Should have failed");
} catch (IllegalStateException expected) {
}
// however, we should be able to set it using hints
query.setHint(QueryHints.HINT_NATIVE_LOCKMODE, LockModeType.READ);
// NOTE : LockModeType.READ should map to LockMode.OPTIMISTIC
assertEquals(LockMode.OPTIMISTIC, query.getLockOptions().getLockMode());
assertNull(query.getLockOptions().getAliasSpecificLockMode("l"));
assertEquals(LockMode.OPTIMISTIC, query.getLockOptions().getEffectiveLockMode("l"));
query.setHint(AvailableSettings.ALIAS_SPECIFIC_LOCK_MODE + ".l", LockModeType.PESSIMISTIC_WRITE);
assertEquals(LockMode.OPTIMISTIC, query.getLockOptions().getLockMode());
assertEquals(LockMode.PESSIMISTIC_WRITE, query.getLockOptions().getAliasSpecificLockMode("l"));
assertEquals(LockMode.PESSIMISTIC_WRITE, query.getLockOptions().getEffectiveLockMode("l"));
em.getTransaction().commit();
em.close();
}
use of org.hibernate.query.NativeQuery in project hibernate-orm by hibernate.
the class NativeQueryDoesNotSupportIterationTest method iterateShouldThrowAnUnsupportedOperationException.
@Test(expected = UnsupportedOperationException.class)
public void iterateShouldThrowAnUnsupportedOperationException() {
try (Session session = openSession()) {
final NativeQuery sqlQuery = session.createNativeQuery("select * from TEST_ENTITY");
sqlQuery.iterate();
}
}
use of org.hibernate.query.NativeQuery in project ice by JBEI.
the class MessageDAO method retrieveNewMessageCount.
public int retrieveNewMessageCount(Account account) {
try {
StringBuilder builder = new StringBuilder();
builder.append("select count(id) from message m where m.is_read=false AND (m.id in ").append("(select message_id from message_destination_accounts where account_id = ").append(account.getId()).append(")");
if (!account.getGroups().isEmpty()) {
builder.append(" OR m.id in (select message_id from message_destination_groups where group_id in (");
int i = 0;
for (Group group : account.getGroups()) {
if (i > 0)
builder.append(", ");
builder.append(group.getId());
i += 1;
}
builder.append("))");
}
builder.append(")");
NativeQuery query = currentSession().createNativeQuery(builder.toString());
Number number = (Number) query.uniqueResult();
return number.intValue();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
}
Aggregations