Example usage for javax.persistence Query getSingleResult

List of usage examples for javax.persistence Query getSingleResult

Introduction

In this page you can find the example usage for javax.persistence Query getSingleResult.

Prototype

Object getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single untyped result.

Usage

From source file:cz.muni.fi.pa165.deliveryservice.dao.jpa.JPACourierDAO.java

@Override
public User getUser(Courier courier) {
    Query query;
    query = em.createQuery("SELECT u FROM User u WHERE u.id = :id ");
    query.setParameter("id", courier.getUser().getId());

    User user = (User) query.getSingleResult();
    return user;//from w ww  .j a  v  a2 s  .c  o  m
}

From source file:org.opentides.dao.impl.UserWidgetsDaoJpaImpl.java

@Override
public long countUserWidgetsColumn(Integer column, long userId) {
    Query query = getEntityManager().createQuery(getJpqlQuery("jpql.userwidgets.countUserWidgetsColumn"));
    query.setParameter("userId", userId);
    query.setParameter("column", column);
    return ((Long) query.getSingleResult()).intValue();
}

From source file:com.music.dao.PieceDao.java

public long getMaxPieceId() {
    Query query = getEntityManager().createQuery("SELECT MAX(id) FROM Piece");
    return (Long) query.getSingleResult();
}

From source file:cz.muni.fi.pa165.deliveryservice.dao.jpa.JPACourierDAO.java

@Override
public Courier findByUsername(String username) {
    Query query = em.createQuery("SELECT c FROM Customer c WHERE c.user.username = :username");
    query.setParameter("username", username);

    Courier courier = (Courier) query.getSingleResult();
    return courier;
}

From source file:org.zkbase.dao.BasicDao.java

public <T> Long count(Class<T> clazz) {
    Query query = entityManager.createQuery("SELECT COUNT(o) FROM " + clazz.getName() + " o");
    return (Long) query.getSingleResult();
}

From source file:org.zkbase.dao.BasicDao.java

/**
 * @return a single object that satisfies the named query.
 * @throws NoResultException//ww w. j ava2s  .  c  o  m
 *             if there is no result
 * @throws NonUniqueResultException
 *             if more than one result
 * @throws IllegalStateException
 *             if called for a Java Persistence query language UPDATE or
 *             DELETE statement
 */
public Object findNamedQuerySingle(String namedQuery, Object... params) {
    Query query = entityManager.createNamedQuery(namedQuery);
    setParameters(query, params);
    return query.getSingleResult();
}

From source file:com.sun.socialsite.business.impl.JPAThemeSettingsManagerImpl.java

public ThemeSettings getThemeSettingsByDestination(String id) throws SocialSiteException {
    if (id == null) {
        throw new SocialSiteException("id is null");
    }//w w  w .j  a v a 2s .  c  om
    Query query = strategy.getNamedQuery("ThemeSettings.getByDestination");
    query.setParameter(1, id);
    try {
        return (ThemeSettings) query.getSingleResult();
    } catch (NonUniqueResultException ne) {
        throw new SocialSiteException("ERROR: more than one destination with id: " + id, ne);
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:net.officefloor.tutorial.transactionhttpserver.TransactionHttpServerTest.java

/**
 * Ensure the JPA connects to database.//ww w .  java 2  s.  c  om
 */
public void testJpa() throws Exception {

    // Request page to allow time for database setup
    this.doRequest("http://localhost:7878/users.woof");

    // Obtain entity manager
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("example");
    EntityManager manager = factory.createEntityManager();

    // Ensure can obtain user and person
    Query query = manager.createQuery("SELECT U FROM User U");
    User user = (User) query.getSingleResult();
    assertEquals("Incorrect user name", "daniel", user.getUserName());
    Person person = user.getPerson();
    assertEquals("Incorrect person name", "Daniel Sagenschneider", person.getFullName());

    // Ensure persist user and person
    User newUser = new User();
    newUser.setUserName("test");
    Person newPerson = new Person();
    newPerson.setFullName("TEST");
    newPerson.setUser(newUser);
    manager.persist(newPerson);
    manager.close();

    // Ensure user and person persisted
    manager = factory.createEntityManager();
    User retrievedUser = manager.find(User.class, newUser.getId());
    assertEquals("Incorrect retrieved user name", "test", retrievedUser.getUserName());
    Person retrievedPerson = retrievedUser.getPerson();
    assertEquals("Incorrect retrieved full name", "TEST", retrievedPerson.getFullName());

    // Close persistence
    factory.close();
}

From source file:eu.xipi.bro4xipi.brokermodel.BrokerJpaController.java

public long countServiceContracts() {
    Query q = entityManager.createQuery("SELECT COUNT(m) FROM ResourceServiceContract m");
    return (Long) q.getSingleResult();
}

From source file:CiudadesApp.Modelo.Actions.VerCiudad_Actions.java

public Ciudad getCiudad(int idCiudad) {

    if (idCiudad == 0) {
        int newIdCiudad = 0;

        Query q = em.createQuery("select max(c.idCiudad) from Ciudad c", Ciudad.class);

        newIdCiudad = (int) q.getSingleResult();

        return ciudadFacade.find(newIdCiudad);

    } else {//ww  w. jav  a  2  s .  c o m
        return ciudadFacade.find(idCiudad);
    }

}