Example usage for javax.persistence TypedQuery getSingleResult

List of usage examples for javax.persistence TypedQuery getSingleResult

Introduction

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

Prototype

X getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single result.

Usage

From source file:com.sshdemo.common.schedule.manage.dao.JobClassDAO.java

public JobClass findByJobClassByClassEntity(final String classEntity) {
    String hql = "From JobClass o Where o.classEntity=:classEntity";

    TypedQuery<JobClass> query = this.getEntityManager().createQuery(hql, JobClass.class);
    query.setParameter("classEntity", classEntity);

    JobClass jobClass = null;//from   w  w w.j av a  2  s.com
    try {
        jobClass = (JobClass) query.getSingleResult();
    } catch (NoResultException e) {
    }

    return jobClass;
}

From source file:com.oreilly.springdata.jpa.core.JpaCustomerRepository.java

@Override
public Customer findByEmailAddress(EmailAddress emailAddress) {

    TypedQuery<Customer> query = em.createQuery("select c from Customer c where c.emailAddress = :email",
            Customer.class);
    query.setParameter("email", emailAddress);

    return query.getSingleResult();
}

From source file:org.osiam.resource_server.storage.dao.ExtensionDao.java

/**
 * Retrieves the extension with the given URN from the database
 *
 * @param urn//  w  w w.ja v a2 s  .c om
 *        the URN of the extension to look up
 * @param caseInsensitive
 *        should the case of the URN be ignored
 * @return the extension entity
 */
public ExtensionEntity getExtensionByUrn(String urn, boolean caseInsensitive) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ExtensionEntity> cq = cb.createQuery(ExtensionEntity.class);
    Root<ExtensionEntity> extension = cq.from(ExtensionEntity.class);

    Predicate predicate;
    if (caseInsensitive) {
        predicate = cb.equal(cb.lower(extension.get(ExtensionEntity_.urn)), urn.toLowerCase(Locale.ENGLISH));
    } else {
        predicate = cb.equal(extension.get(ExtensionEntity_.urn), urn);
    }

    cq.select(extension).where(predicate);

    TypedQuery<ExtensionEntity> query = em.createQuery(cq);

    ExtensionEntity singleExtension;

    try {
        singleExtension = query.getSingleResult();
    } catch (NoResultException e) {
        throw new OsiamException("Could not find the Extension '" + urn + "'.", e);
    }

    return singleExtension;
}

From source file:br.com.semanticwot.cd.daos.SwotApplicationDAO.java

@Override
public SwotApplication findByName(String name) {
    TypedQuery<SwotApplication> query = entityManager
            .createQuery("select distinct(p) from SwotApplication p where p.name=:name", SwotApplication.class)
            .setParameter("name", name);
    return query.getSingleResult();
}

From source file:com.epam.ipodromproject.repository.jpa.JPAUserRepository.java

@Override
public User getUserByUsername(String username) {
    TypedQuery<User> query = entityManager.createNamedQuery("User.findByUsername", User.class);
    query.setParameter("username", username);
    return query.getSingleResult();
}

From source file:kirchnerei.note.model.DataService.java

private <T> T getSingleResult(TypedQuery<T> q) {
    try {//from www . ja  v  a  2  s .  c o  m
        return q.getSingleResult();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.epam.ipodromproject.repository.jpa.JPAHorseRepository.java

@Override
public Horse getHorseByName(String name) {
    TypedQuery query = entityManager.createNamedQuery("Horse.findByName", Horse.class);
    query.setParameter("name", name);
    Horse horse = null;//w  w  w .  j a  va2s  . co m
    try {
        horse = (Horse) query.getSingleResult();
    } catch (Exception e) {

    }
    return horse;
}

From source file:com.siriusit.spezg.multilib.storage.jpa.JpaBinaryAttachmentRepository.java

@Override
public BinaryAttachment findBinaryAttachmentByUnit(Unit unit) {
    TypedQuery<BinaryAttachmentDomainObject> query = entityManager
            .createNamedQuery("findAttachmentByUnitAndType", BinaryAttachmentDomainObject.class);
    query.setParameter("unit", unit);
    return query.getSingleResult();
}

From source file:com.siriusit.spezg.multilib.storage.jpa.JpaPersonRepository.java

@Override
public Person findPersonByUsername(String username) {
    TypedQuery<PersonDomainObject> query = entityManager.createNamedQuery("findPersonByUsername",
            PersonDomainObject.class);
    query.setParameter("username", username);
    return query.getSingleResult();
}

From source file:org.verinice.persistence.VeriniceAccountDaoImpl.java

private Entity findEntityByLoginName(String loginName) {
    TypedQuery<Entity> propertiesQuery = buildQueryForProperties(loginName);
    Entity entity = null;//from  w w w.  j a v a 2 s . c  o m
    try {
        entity = propertiesQuery.getSingleResult();
    } catch (NoResultException ex) {
        LOG.info("Account with login name: '" + loginName + "' not found");
        LOG.debug("Stacktrace: ", ex);
    }
    return entity;
}