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.all.mobile.web.services.TestTopHundredService.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/* w ww  .  j a v  a2s.c  om*/
public void shouldGetTopPlaylistsForCategory() throws Exception {
    Long categoryId = 1L;
    service.getTopPlaylistsByCategory(categoryId);

    verify(hibernateTemplate).executeFind(callbackCaptor.capture());
    HibernateCallback<?> callback = callbackCaptor.getValue();
    Session session = mock(Session.class);
    Query query = mock(Query.class);
    when(session.createQuery(anyString())).thenReturn(query);
    Category category = mock(Category.class);
    when(query.uniqueResult()).thenReturn(category);
    List<TopPlaylist> expectedList = new ArrayList<TopPlaylist>();
    expectedList.add(new TopPlaylist("some playlist"));
    when(category.list()).thenReturn(expectedList);
    List<TopPlaylist> resultList = (List<TopPlaylist>) callback.doInHibernate(session);

    verify(query).setParameter("categoryId", categoryId);
    assertEquals(expectedList, resultList);
}

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

License:Apache License

@SuppressWarnings("unchecked")
@Test//from ww w .  jav a2  s  .c o  m
public void shouldReturnEmptyListIfInvalidCategory() throws Exception {
    Long categoryId = 1L;
    service.getTopPlaylistsByCategory(categoryId);

    verify(hibernateTemplate).executeFind(callbackCaptor.capture());
    HibernateCallback<?> callback = callbackCaptor.getValue();
    Session session = mock(Session.class);
    Query query = mock(Query.class);
    when(session.createQuery(anyString())).thenReturn(query);
    when(query.uniqueResult()).thenReturn(null);
    List<TopPlaylist> resultList = (List<TopPlaylist>) callback.doInHibernate(session);

    verify(query).setParameter("categoryId", categoryId);
    assertEquals(Collections.emptyList(), resultList);
}

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

License:Apache License

@SuppressWarnings("unchecked")
public List<TopPlaylist> getTopPlaylistsByCategory(final Long categoryId) {
    return hibernateTemplate.executeFind(new HibernateCallback<List<TopPlaylist>>() {
        @Override//from   www .  j a  v  a2  s.  co m
        public List<TopPlaylist> doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("From Category where id=:categoryId");
            query.setParameter("categoryId", categoryId);
            Category category = (Category) query.uniqueResult();
            return (List<TopPlaylist>) (category != null ? category.list() : Collections.emptyList());
        }
    });
}

From source file:com.all.rds.service.impl.TopHundredServiceImpl.java

License:Apache License

@SuppressWarnings("unchecked")
public List<TopPlaylist> getTopPlaylists(final Long categoryId) {
    return hibernateTemplate.executeFind(new HibernateCallback<List<TopPlaylist>>() {
        @Override/*w  ww .j  a v  a 2 s  . c  o  m*/
        public List<TopPlaylist> doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("From Category where id=:categoryId");
            query.setParameter("categoryId", categoryId);
            Category category = (Category) query.uniqueResult();
            return (List<TopPlaylist>) (category != null ? category.list() : Collections.emptyList());
        }
    });
}

From source file:com.all.rds.service.impl.TopHundredServiceImpl.java

License:Apache License

@Override
public TopPlaylist getRandomTopPlaylist() {
    return hibernateTemplate.execute(new HibernateCallback<TopPlaylist>() {
        @Override/*w w w .j a  v  a 2s  .c om*/
        public TopPlaylist doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("From TopPlaylist order by rand()");
            query.setMaxResults(1);
            TopPlaylist topPlaylist = (TopPlaylist) query.uniqueResult();
            query = session.createQuery(
                    "Select ct From CachedTrack ct, TopPlaylistTrack tpt where tpt.playlist=:topPlaylistId and tpt.track=ct.hashcode order by tpt.numTrack");
            query.setParameter("topPlaylistId", topPlaylist.getHashcode());
            @SuppressWarnings("unchecked")
            List<CachedTrack> tracks = query.list();
            topPlaylist.setTracks(tracks);
            return topPlaylist;
        }
    });
}

From source file:com.all.uberpeer.persistence.ContactDao.java

License:Apache License

public ContactInfo findContactByEmail(final String email) {
    return ht.execute(new HibernateCallback<ContactInfo>() {
        @Override/*ww w. ja v a 2  s .co  m*/
        public ContactInfo doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from User u where u.email=:email");
            query.setString("email", email);
            User user = (User) query.uniqueResult();
            if (user != null) {
                return new ContactInfo(user);
            }
            return null;
        }
    });

}

From source file:com.all.uberpeer.persistence.LibraryDao.java

License:Apache License

public LibrarySnapshot findSnapshotByUserAndVersion(final String owner, final Integer version) {
    return ht.execute(new HibernateCallback<LibrarySnapshot>() {
        @Override/*  ww  w  . j av a 2  s  . c o m*/
        public LibrarySnapshot doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("From LibrarySnapshot where email=:owner  and version=:version");
            query.setParameter("owner", owner);
            query.setParameter("version", version);
            return (LibrarySnapshot) query.uniqueResult();
        }
    });
}

From source file:com.all.uberpeer.persistence.TrackDao.java

License:Apache License

public MusicTrack findTrackByHashcode(final String hashcode) {
    return ht.execute(new HibernateCallback<MusicTrack>() {
        @Override//from   w  ww  .  ja  v a  2 s.  c o m
        public MusicTrack doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from MUSIC_TRACKS u where u.hashcode=:hashcode");
            query.setString("hashcode", hashcode);
            MusicTrack track = (MusicTrack) query.uniqueResult();
            return track;
        }
    });

}

From source file:com.Allaction.java

public Querydata showquery() {
    Session session = null;/* w ww . j a v  a 2  s.  c  o m*/
    Transaction tx = null;
    Querydata data = null;
    try {
        session = MyHibernateUtilQuery.getSession();
        tx = session.getTransaction();
        tx.begin();
        Query q = session.createQuery("from Querydata");
        data = (Querydata) q.uniqueResult();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return data;
}

From source file:com.Allaction.java

public Blogdata showblog() {
    Session session = null;/*from ww w.j a  va 2  s. c  o  m*/
    Transaction tx = null;
    Blogdata data = null;
    try {
        session = MyHibernateUtilBlog.getSession();
        tx = session.getTransaction();
        tx.begin();
        Query q = session.createQuery("from Blogdata");
        data = (Blogdata) q.uniqueResult();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return data;
}