Java Code Examples for org.hibernate.query.NativeQuery#getSingleResult()

The following examples show how to use org.hibernate.query.NativeQuery#getSingleResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NamedQueryIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenNamedNativeQueryIsCalledUsingGetNamedNativeQuery_ThenOk() {
    @SuppressWarnings("rawtypes")
    NativeQuery query = session.getNamedNativeQuery("DeptEmployee_FindByEmployeeName");
    query.setParameter("name", "John Wayne");
    DeptEmployee result = (DeptEmployee) query.getSingleResult();
    Assert.assertNotNull(result);
    Assert.assertEquals("001", result.getEmployeeNumber());
}
 
Example 2
Source File: NativeQueryMethodInterceptorImpl.java    From spring-native-query with MIT License 4 votes vote down vote up
private Object executeWithEntityManager(NativeQueryInfo info) {
    EntityManager entityManager = ApplicationContextProvider.getApplicationContext().getBean(EntityManager.class);
    Session session = entityManager.unwrap(Session.class);
    NativeQuery<?> query;
    if (info.isEntity()) {
        query = session.createNativeQuery(info.getSql(), info.getAliasToBean());
    } else {
        query = session.createNativeQuery(info.getSql());
    }

    addParameterJpa(query, info);

    if (info.hasPagination()) {
        query.setFirstResult(info.getFirstResult());
        query.setMaxResults(info.getMaxResult());
    }

    query.getQueryString();

    if (!info.isJavaObject() && !info.isEntity()) {
        query.setResultTransformer(Transformers.aliasToBean(info.getAliasToBean()));
    }
    if (info.getReturnType().getSimpleName().equals(Void.TYPE.getName())) {
        query.executeUpdate();
        return null;
    }

    if (info.returnTypeIsOptional()) {
        return getOptionalReturn(query::getSingleResult);
    }

    if (info.isSingleResult()) {
        return query.getSingleResult();
    }

    List<?> resultList = query.list();
    if (info.isPagination()) {
        return new PageImpl(resultList, info.getPageable(), getTotalRecords(info, session));
    }
    return resultList;
}
 
Example 3
Source File: NativeQueryMethodInterceptorImpl.java    From spring-native-query with MIT License 4 votes vote down vote up
private Long getTotalRecords(NativeQueryInfo info, Session session) {
    NativeQuery<?> query = session.createNativeQuery(info.getSqlTotalRecord());
    query.unwrap(NativeQuery.class).addScalar("totalRecords", LongType.INSTANCE);
    addParameterJpa(query, info);
    return (Long) query.getSingleResult();
}