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:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public long searchCount(T condition) {
    StringBuilder qString = new StringBuilder(
            "select count(model) from " + entityClass.getSimpleName() + " model");
    StringBuilder qWhere = new StringBuilder(" where ");
    StringBuilder qCondition = new StringBuilder();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(entityClass);
    for (int i = 0, count = propertyDescriptors.length; i < count; i++) {
        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
        String name = propertyDescriptor.getName();
        Class<?> type = propertyDescriptor.getPropertyType();
        String value = null;/*ww w  .jav  a2  s.  co m*/
        try {
            value = BeanUtils.getProperty(condition, name);
        } catch (Exception e) {
            // ?
            continue;
        }
        if (value == null || name.equals("class")) {
            continue;
        }
        if ("java.lang.String".equals(type.getName())) {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append(" like ");
            qCondition.append("'%");
            qCondition.append(value);
            qCondition.append("%'");
        } else {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append("=");
            qCondition.append(value);
        }
        qCondition.append(" and ");
    }
    if (qCondition.length() != 0) {
        qString.append(qWhere).append(qCondition);
        if (qCondition.toString().endsWith(" and ")) {
            qString.delete(qString.length() - " and ".length(), qString.length());
        }
    }
    Query query = em.createQuery(qString.toString());
    return (Long) query.getSingleResult();
}

From source file:net.chrissearle.flickrvote.dao.JpaChallengeDao.java

/**
 * Method getMostRecent returns the challenge with the latest start date.
 *
 * @return the challenge with the latest start date.
 *///  w  w w.j  a v  a 2  s.com
public Challenge getMostRecent() {
    Query query = entityManager
            .createQuery("select c from Challenge c where c.startDate = (select MAX(c.startDate) FROM c)");

    try {
        return (Challenge) query.getSingleResult();
    } catch (NoResultException e) {
        // Means nothing - just that the db is empty
        return null;
    }
}

From source file:de.voolk.marbles.persistence.services.PageService.java

@Override
public Page getRootPage(IUser user) {
    Query query = getEntityManager().createNamedQuery("page:rootPage");
    query.setParameter("user", user);
    try {/*from   w w  w  .j ava 2  s  .c  o  m*/
        return (Page) query.getSingleResult();
    } catch (NoResultException e) {
        Page page = new Page();
        page.setName("ROOT");
        StringBuilder sb = new StringBuilder();
        sb.append("Root Page").append("\n").append("---------").append("\n\n");
        sb.append("This is the ROOT Page please edit ...").append("\n\n");
        sb.append("%%ThisIsASubpage").append("\n\n");
        sb.append("This is a list:").append("\n\n");
        sb.append("* item1").append("\n");
        sb.append("* item2").append("\n");
        sb.append("* item3").append("\n");
        sb.append("\n");
        sb.append("for more see [Markdown](http://en.wikipedia.org/wiki/Markdown)");
        page.setContent(sb.toString());
        page.setUser(resolve(User.class, user));
        persist(page);
        return page;
    }
}

From source file:cz.fi.muni.pa165.dao.PrintedBookDAOImpl.java

@Override
public PrintedBook findPrintedBookById(long id) {
    try {/*w ww .  j a v a 2s. c  om*/
        final Query query = em.createQuery("SELECT m FROM PrintedBook as m WHERE m.idPrintedBook = :i");
        query.setParameter("i", id);
        return (PrintedBook) query.getSingleResult();
    } catch (RuntimeException E) {
        throw new DAException(E.getMessage());
    }
}

From source file:ch.puzzle.itc.mobiliar.business.template.boundary.TemplateEditor.java

@TransactionAttribute(TransactionAttributeType.REQUIRED)
Object getSingleObjectOrNull(Query q) {
    try {/*  www.  j  av  a  2 s.c  om*/
        return q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.cimpoint.mes.server.repositories.UnitRepository.java

public String getNextUnitSerialNumber() throws Exception {
    Query qry = getEntityManager().createQuery("select o from EUnit o order by o.serialNumber desc")
            .setMaxResults(1);/*from w  ww .j  a va 2s .co  m*/
    try {
        EUnit Unit = (EUnit) qry.getSingleResult();
        return String.valueOf(Integer.parseInt(Unit.getSerialNumber()) + 1);
    } catch (NoResultException ex) {
        return this.getInitUnitSerialNumber();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}

From source file:com.github.peholmst.springsecuritydemo.services.impl.CategoryServiceImpl.java

/**
 * TODO Document me!/*from   w  w  w . j  a v a 2s  . c  o m*/
 * 
 * @param category
 * @return
 */
protected boolean containsCategory(Category category) {
    assert category != null : "category must not be null";
    if (category.getId() == null) {
        return false;
    } else {
        Query query = getEntityManager().createQuery("SELECT count(c) FROM Category c WHERE c.id = :id");
        query.setParameter("id", category.getId());
        Object result = query.getSingleResult();
        if (result instanceof Integer) {
            return ((Integer) result).intValue() == 1;
        } else {
            return ((Long) result).longValue() == 1;
        }
    }
}

From source file:de.voolk.marbles.persistence.services.PageService.java

@Override
public Page findPageByUserAndId(IUser user, int id) {
    Query query = getEntityManager().createNamedQuery("page:byUserAndId");
    query.setParameter("user", user);
    query.setParameter("id", id);
    return (Page) query.getSingleResult();
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.ClaimDAOJPAImpl.java

@Override
public void deleteClaim(String claimType) {
    Query query = null;
    query = em.createQuery("select c from Claim c where c.claimType=:claimtype");
    query.setParameter("claimtype", claimType);

    //@SuppressWarnings("rawtypes")
    Object claimObj = query.getSingleResult();
    em.remove(claimObj);/*from ww w  . ja v  a2 s  . c o  m*/

    LOG.debug("Claim '{}' deleted", claimType);
}

From source file:com.cimpoint.mes.server.repositories.WorkOrderRepository.java

public String getNextWorkOrderItemNumber() throws Exception {

    Query qry = getEntityManager().createQuery("select o from EWorkOrderItem o order by o.number desc")
            .setMaxResults(1);// ww  w.  ja  va  2 s .c  om
    try {
        EWorkOrderItem woi = (EWorkOrderItem) qry.getSingleResult();
        return String.valueOf(Integer.parseInt(woi.getNumber()) + 1);
    } catch (NoResultException ex) {
        return this.getInitWorkOrderNumber();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}