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:modelo.dao.GestionProductosImpl.java

@Override
public Producto obtenerProducto(String referencia, int usuario) {
    Query q = em.createNamedQuery("Producto.findByReferencia");
    q.setParameter(1, referencia);//  www. j  a  v a2s.com
    q.setParameter(2, usuario);
    try {
        return (Producto) q.getSingleResult();
    } catch (Exception e) {
        return new Producto();

    }
}

From source file:info.toegepaste.www.service.PdfServiceImpl.java

@Override
@TransactionAttribute(REQUIRES_NEW)/*from   ww  w. j av a  2s  . c  o m*/
public Student getStudentById(int id) {
    Student student = null;
    Query q = em.createNamedQuery("Student.findByStudentid");
    q.setParameter("studentid", id);
    student = (Student) q.getSingleResult();
    return student;
}

From source file:org.fornax.cartridges.sculptor.framework.test.AbstractDbUnitJpaTests.java

/**
 * Using a separate JDBC connection causes locking problems with different rdbms
 * (hsqldb 2.x introduced a new transaction, locking and isolation level handling)
 *///ww w .  j a v a  2s. c o m
@Override
protected int countRowsInTable(String tableName, String additionalCondition) {
    flush();
    clear();
    Query query = entityManager
            .createNativeQuery("select count(*) from " + tableName + " " + additionalCondition);
    Number rowCount = (Number) query.getSingleResult();
    return rowCount.intValue();
}

From source file:org.kuali.mobility.push.dao.PushDeviceTupleDaoImplTest.java

@Test
@DirtiesContext//from   w w  w. j  a va  2  s . c o m
public void countUnsentTuples() {
    Query query = getEntityManager().createNamedQuery("PushDeviceTuple.countUnsent");
    Long count = (Long) query.getSingleResult();
    LOG.debug("There are " + count.toString() + " unsent tuples in the database.");
    assertTrue("Count was null and should never be.", count != null);
}

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

public final boolean isRegisteredByEmail(String emailAddress) {
    if (StringUtil.isEmpty(emailAddress))
        return false;
    String queryString = getJpqlQuery("jpql.user.countByEmailAddress");
    Query queryObject = getEntityManager().createQuery(queryString);
    queryObject.setParameter("emailAddress", emailAddress);
    long count = (Long) queryObject.getSingleResult();
    return count != 0;
}

From source file:pl.com.bottega.ecommerce.businessprocess.ordertracking.OrderShipmentStatusTrackerSagaManager.java

private OrderShipmentStatusTrackerData findByOrderId(AggregateId orderId) {
    Query query = entityManager.createQuery("from OrderShipmentStatusTrackerData where orderId=:orderId")
            .setParameter("orderId", orderId);
    return (OrderShipmentStatusTrackerData) query.getSingleResult();
}

From source file:com.branded.holdings.qpc.repository.jpa.JpaOwnerRepositoryImpl.java

@Override
public Owner findById(int id) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em
            .createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id");
    query.setParameter("id", id);
    return (Owner) query.getSingleResult();
}

From source file:pl.edu.uksw.j2eecourse.service.BookRentalServiceImpl.java

@Override
public Book findByIsbn(String isbn) {
    Query q = entityManager.createQuery("Select b from Book b where b.isbn = :isbn");
    q.setParameter("isbn", isbn);
    try {/*from   w w  w. ja  va 2  s  .  c  o m*/
        return (Book) q.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }
}

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

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

From source file:modelo.dao.GestionHistoricoImpl.java

@Override
public Historico obtenerUltimoHistorico(String referencia, int usuario) {
    Query q = em.createNamedQuery("Historico.findLastDateByRef");
    q.setParameter(1, referencia);//from   w w w  .  java  2s .com
    q.setParameter(2, usuario);
    q.setMaxResults(1);
    try {
        return (Historico) q.getSingleResult();
    } catch (Exception e) {
        return new Historico(null);
    }
}