Example usage for org.hibernate Query uniqueResult

List of usage examples for org.hibernate Query uniqueResult

Introduction

In this page you can find the example usage for org.hibernate Query uniqueResult.

Prototype

R uniqueResult();

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:com.alex.web.proyectofinal.DAOUsuarioImpl.java

/**
 * Este metodo busca un cliente por medio de su Id
 * @param id Este parametro es el Id del cliente que se quiere buscar
 * @return El tipo de retorno es el cliente buscado
 *///  w  w  w .j  a va2  s.  c  o  m
public Usuario buscarPorId(int id) {
    begin();
    Query q = getSession().createQuery("from Cliente where id = :id");
    q.setInteger("id", id);
    Usuario p = (Usuario) q.uniqueResult();
    commit();
    close();
    return p;

}

From source file:com.all.backend.web.persistence.TestLibraryDao.java

License:Apache License

@Test
public void shouldSaveDelta() throws Exception {
    Long snapshotId = 100L;/*from  w ww.  ja va2  s.c o  m*/
    Long lastDelta = 3L;
    SyncValueObject deltaObject = new SyncValueObject("user@all.com", 1, 0, 1);
    libraryDao.saveDelta(snapshotId, deltaObject);

    verify(hibernateTemplate).execute(callbackCaptor.capture());

    HibernateCallback<?> callback = callbackCaptor.getValue();
    Query statusQuery = mock(Query.class);
    when(session.createQuery(anyString())).thenReturn(query, statusQuery);
    when(query.uniqueResult()).thenReturn(lastDelta);
    LibrarySyncStatus syncStatus = mock(LibrarySyncStatus.class);
    when(statusQuery.uniqueResult()).thenReturn(syncStatus);

    LibraryDelta savedDelta = (LibraryDelta) callback.doInHibernate(session);

    verify(query).setParameter("snapshotId", snapshotId);
    verify(query).uniqueResult();
    verify(session).save(any(LibraryDelta.class));
    assertEquals(new Integer(lastDelta.intValue() + 1), savedDelta.getVersion());
    verify(statusQuery).setParameter("owner", deltaObject.getEmail());
    verify(statusQuery).uniqueResult();
    verify(syncStatus).setCurrentDelta(savedDelta.getVersion());
    verify(session).saveOrUpdate(syncStatus);
}

From source file:com.all.client.model.LocalModelDao.java

License:Apache License

public long count(final Class<?> objectClass) {

    return hibernateTemplate.execute(new HibernateCallback<Long>() {
        @Override//from w w w. j  a  va  2s .  c  o m
        public Long doInHibernate(Session session) throws SQLException {
            String entityName = objectClass.getSimpleName();
            if (objectClass.isAnnotationPresent(Entity.class)) {
                Entity entity = objectClass.getAnnotation(Entity.class);
                if (StringUtils.isNotEmpty(entity.name())) {
                    entityName = entity.name();
                }
            }
            Query q = session.createQuery("select count(*) from " + entityName);
            return Long.valueOf(q.uniqueResult().toString());
        }
    });
}

From source file:com.all.client.model.LocalModelDao.java

License:Apache License

@SuppressWarnings("unchecked")
public int count(final String hql) {
    return ((Long) hibernateTemplate.execute(new HibernateCallback() {
        @Override/*from   ww  w . j av  a  2s  .  co  m*/
        public Object doInHibernate(Session session) throws SQLException {
            Query q = session.createQuery("select count(*) " + hql);
            return q.uniqueResult();
        }
    })).intValue();
}

From source file:com.all.client.model.LocalModelDao.java

License:Apache License

@SuppressWarnings("unchecked")
public int countLooseTracks() {
    return ((Integer) hibernateTemplate.execute(new HibernateCallback() {
        @Override/*  www .j  a v a  2  s .  co  m*/
        public Object doInHibernate(Session session) throws SQLException {
            Query q = session.createSQLQuery("SELECT COUNT (track.hashcode) looseTracks "
                    + " FROM Track track LEFT JOIN PlaylistTrack playlistTrack ON playlistTrack.trackfk = track.hashcode "
                    + " WHERE playlistTrack.id is null  ");
            return q.uniqueResult();
        }
    })).intValue();
}

From source file:com.all.client.model.LocalModelDao.java

License:Apache License

@SuppressWarnings("unchecked")
public Object findAll(final String hql, final Map<String, Object> values, final boolean includeAllResults) {
    Object result = hibernateTemplate.execute(new HibernateCallback() {
        @Override//from ww  w .  j ava 2 s . c o  m
        public Object doInHibernate(Session session) throws SQLException {
            Query q = session.createQuery(hql);
            q.setCacheable(true);
            if (values != null) {
                for (String param : values.keySet()) {
                    q.setParameter(param, values.get(param));
                }
            }
            if (includeAllResults) {
                return q.list();
            } else {
                return q.uniqueResult();
            }
        }
    });

    return result;
}

From source file:com.all.mobile.web.persistence.WidgetDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<MobileTrack> getPlaylist(final Long playlistId) {
    return hibernate.executeFind(new HibernateCallback<List<MobileTrack>>() {
        @Override//from  w  w  w . j a  v  a  2 s  . c o m
        public List<MobileTrack> doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from WidgetContent w where w.widgetId=:playlistId");
            query.setLong("playlistId", playlistId);
            WidgetContent widgetContent = (WidgetContent) query.uniqueResult();
            return widgetContent.getPlaylist();
        }
    });
}

From source file:com.all.mobile.web.services.CacheService.java

License:Apache License

private String getFileExtension(final String trackId) {
    return hibernate.execute(new HibernateCallback<String>() {
        @Override/*from  w w  w.  j  ava 2 s  .c  om*/
        public String doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("SELECT fileFormat FROM MobileTrack WHERE hashcode=:trackId");
            query.setParameter("trackId", trackId);
            String type = query.uniqueResult().toString();
            if (!type.startsWith(".")) {
                type = "." + type;
            }
            return type.toLowerCase();
        }
    });
}

From source file:com.all.mobile.web.services.TestCacheService.java

License:Apache License

private void verifyFileExtensionCallback(HibernateCallback<?> callback)
        throws HibernateException, SQLException {
    Session session = mock(Session.class);
    Query query = mock(Query.class);
    when(session.createQuery(anyString())).thenReturn(query);
    when(query.uniqueResult()).thenReturn("MP3");
    Object result = callback.doInHibernate(session);
    verify(query).setParameter(eq("trackId"), eq(trackId));

    assertEquals(extension, result);// w w  w.jav  a 2 s.  c  o  m
}

From source file:com.all.mobile.web.services.TestSearchService.java

License:Apache License

private HibernateCallback<?> executeHibernateCallback() {

    service.initialize();/*  ww w  . ja  v  a  2s. co m*/

    verify(hibernate).execute(callbackCaptor.capture());

    HibernateCallback<?> createLuceneIndexes = callbackCaptor.getValue();

    when(fullTextSessionfactory.createFullTextSession(session)).thenReturn(fullTextSession);
    Query query = mock(Query.class);
    when(session.createQuery(anyString())).thenReturn(query);
    when(query.uniqueResult()).thenReturn(now);
    return createLuceneIndexes;
}