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:info.toegepaste.www.service.PdfServiceImpl.java

@Override
@TransactionAttribute(REQUIRES_NEW)//from   w  w  w .j  a v  a 2s  .c o  m
public Test getTestById(int id) {
    Test test = null;
    Query q = em.createNamedQuery("Test.findByTestid");
    q.setParameter("testid", id);
    test = (Test) q.getSingleResult();
    return test;
}

From source file:org.makersoft.activerecord.unit.JPATest.java

@Test
@Transactional/*  w  w w. ja v a2 s  . c om*/
public void testInit() {

    Assert.assertNotNull(entityManagerFactory);
    Assert.assertNotNull(em);
    Assert.assertNotNull(JPA.em());

    final UserModel user = new UserModel();
    user.username = "makersoft";
    user.password = "fengkuok";

    em.persist(user);
    em.remove(em.find(UserModel.class, user.id));
    em.merge(user);

    System.out.println(em.contains(user));

    Query q = em.createQuery("select count(*) from UserModel");
    System.out.println(q.getSingleResult());

    System.out.println(user.getId());
}

From source file:DAO.FichiersDAOImpl.java

@Transactional(readOnly = true)
@Override//from   www . j ava2  s . c  om
public FichiersEntity find(int id) {
    Query q = em.createQuery("SELECT f FROM FichiersEntity f where f.id = ?");
    q.setParameter(1, id);
    try {
        return (FichiersEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:vlove.dao.impl.GenericDaoImpl.java

/**
 * @see vlove.dao.GenericDao#querySingle(String, Map)
 *///from  ww  w  .ja v  a2s . c  o  m
@Override
public <T> T querySingle(String query, Map<String, Object> parameters) {
    Query q = em.createQuery(query);
    applyParameters(q, parameters);
    return (T) q.getSingleResult();
}

From source file:com.healthcit.cacure.dao.ModuleDao.java

/**
 * Deletes only module with empty forms, otherwise throws NoResultException exception.
 * @param moduleId Long//  ww w . j a v  a  2s  .  c o m
 */
public void deleteModuleWithEmptyForms(Long moduleId) {
    //prevent from deleting a Module item that has forms.
    //This scenario may appear by editing the URL.
    String jpql = "select m from BaseModule m left join m.forms f " + "where m.id = :moduleId and f is null";
    Query query = em.createQuery(jpql);
    query.setParameter("moduleId", moduleId);
    try {
        BaseModule module = (BaseModule) query.getSingleResult();
        delete(module);
    } catch (NoResultException e) {
        logger.info("try to delete an not empty module");
    }
}

From source file:org.kuali.mobility.user.dao.UserDaoImpl.java

public User findUserByUserId(String userId) {
    Query query = entityManager.createQuery("select u from User u where u.userId = :userId");
    query.setParameter("userId", userId);
    try {//w  w w . j a va2  s .  co m
        return (User) query.getSingleResult();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.gerenciaProyecto.DaoImple.UsuarioDaoImpl.java

@Override
public Usuario consultaUsuarioLogin(String login) {
    EntityManager em = getEntityManager();
    Usuario usuarioLogin = null;//from   www  . j ava2s . c o m
    try {
        Query q = em.createQuery("SELECT u FROM Usuario u WHERE u.username = :username");
        q.setParameter("username", login);
        usuarioLogin = (Usuario) q.getSingleResult();
    } catch (NoResultException e) {
        e.printStackTrace();
    }
    return usuarioLogin;
}

From source file:com.june.app.site.repository.jpa.VideoRepositoryImpl.java

@Override
public Video videoGet(Integer nttId) {
    Query query = this.em.createQuery("SELECT video FROM Video video WHERE video.nttId =:nttId");
    query.setParameter("nttId", nttId);
    return (Video) query.getSingleResult();
}

From source file:fr.norad.visuwall.core.persistence.dao.WallDAOImpl.java

@Override
public Wall find(String wallName) throws NotFoundException {
    Query query = entityManager.createNamedQuery(Wall.QUERY_WALLBYNAME);
    query.setParameter(Wall.QUERY_PARAM_NAME, wallName);
    Wall wall = (Wall) query.getSingleResult();

    // TODO replace with lazy load with extended entityManager or eager request 
    for (SoftwareAccess softwareInfo : wall.getSoftwareAccesses()) {
        softwareInfo.getProjectIds();//from   www .j  a  v a2s . c  o m
        softwareInfo.getViewNames();
    }
    return wall;
}

From source file:gemlite.core.internal.support.jpa.files.dao.GmBatchDaoImpl.java

@Override
public int countRunningJob() {
    String countRunningJob = "select count(1) from batch_job_execution where status = 'STARTED'";
    Query query = em.createNativeQuery(countRunningJob);
    BigInteger count = (BigInteger) query.getSingleResult();
    return count.intValue();
}