Search in sources :

Example 6 with NativeQuery

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();
}
Also used : InOrder(org.mockito.InOrder) Transaction(org.hibernate.Transaction) NativeQuery(org.hibernate.query.NativeQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with NativeQuery

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();
}
Also used : EntityManager(javax.persistence.EntityManager) NativeQuery(org.hibernate.query.NativeQuery) Test(org.junit.Test)

Example 8 with NativeQuery

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();
    }
}
Also used : NativeQuery(org.hibernate.query.NativeQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with NativeQuery

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);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Group(org.jbei.ice.storage.model.Group) NativeQuery(org.hibernate.query.NativeQuery) HibernateException(org.hibernate.HibernateException)

Aggregations

NativeQuery (org.hibernate.query.NativeQuery)9 HibernateException (org.hibernate.HibernateException)5 Session (org.hibernate.Session)5 Test (org.junit.Test)5 DAOException (org.jbei.ice.storage.DAOException)4 Group (org.jbei.ice.storage.model.Group)3 List (java.util.List)2 EntityManager (javax.persistence.EntityManager)2 Transaction (org.hibernate.Transaction)2 InOrder (org.mockito.InOrder)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NativeQueryImplementor (org.hibernate.query.spi.NativeQueryImplementor)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Message (org.jbei.ice.storage.model.Message)1