Example usage for javax.persistence EntityManager createNamedQuery

List of usage examples for javax.persistence EntityManager createNamedQuery

Introduction

In this page you can find the example usage for javax.persistence EntityManager createNamedQuery.

Prototype

public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass);

Source Link

Document

Create an instance of TypedQuery for executing a Java Persistence query language named query.

Usage

From source file:org.exoplatform.task.dao.jpa.StatusDAOImpl.java

public List<Status> findByNamedQuery(String query, Map<String, Object> params) {
    EntityManager em = getEntityManager();
    TypedQuery<Status> q = em.createNamedQuery(query, Status.class);
    if (params != null) {
        for (Map.Entry<String, Object> p : params.entrySet()) {
            q.setParameter(p.getKey(), p.getValue());
        }/*from w  w  w. j ava  2 s .c  o m*/
    }
    return q.getResultList();
}

From source file:ro.allevo.fintpws.security.CustomUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    EntityManagerFactory configEntityManagerFactory = Persistence
            .createEntityManagerFactory(ApiResource.PERSISTENCE_UNITNAME_CONFIG);
    EntityManager emc = configEntityManagerFactory.createEntityManager();

    final TypedQuery<UserEntity> query = emc.createNamedQuery("UserEntity.findByUsername", UserEntity.class);

    final java.util.List<UserEntity> results = query.setParameter("username", username).getResultList();

    if (!results.isEmpty()) {
        return results.get(0);
    }/* w  w w . j a  va2s.com*/
    return null;

}

From source file:br.nom.abdon.gastoso.dal.ContasDao.java

@Override
protected void prepararDelecao(final EntityManager em, final Conta conta) throws DalException {

    final Boolean temLancamentos = em.createNamedQuery("Conta.temLancamento", Boolean.class)
            .setParameter("conta", conta).getSingleResult();

    if (temLancamentos) {
        throw new DalException(ERRO_TEM_LANCAMENTOS);
    }//  w w  w.j a v  a  2s  .c  o  m
}

From source file:com.chiralbehaviors.CoRE.agency.AgencyNetwork.java

public List<Relationship> getUsedRelationships(EntityManager em) {
    return em.createNamedQuery(GET_USED_RELATIONSHIPS, Relationship.class).getResultList();
}

From source file:ch.bfh.srs.srv.service.ReservationServiceTest.java

@Test(expected = NotImplementedException.class)
public void testAddExclusion() {
    boolean added = service.addExclusion(1, DateTime.now());
    assertTrue(added);/*w  w w . j  a  va  2s .co  m*/
    EntityManager em = service.getEntityManager();
    Exclusion exclusion = em.createNamedQuery(Exclusion.ID_NQUERY, Exclusion.class).setParameter("id", 1)
            .getSingleResult();
    assertNotNull(exclusion);
}

From source file:comp.web.core.DataUtil.java

public List<Product> getProds(String cat, String prod, String from, String to) {
    logger.log(Level.FINER, "get prods with filter {0} {1} {2} {3}", new Object[] { cat, prod, from, to });

    if (StringUtils.isBlank(cat) && StringUtils.isBlank(prod) && StringUtils.isBlank(from)
            && StringUtils.isBlank(to)) {
        return Collections.emptyList();
    }/*from  ww w .j a  v a2s .com*/

    String cat1 = StringUtils.stripToEmpty(cat) + "%";
    String prod1 = StringUtils.stripToEmpty(prod) + "%";
    double from1 = StringUtils.isNumeric(from) ? Double.parseDouble(from) : Double.MIN_VALUE;
    double to1 = StringUtils.isNumeric(to) ? Double.parseDouble(to) : Double.MAX_VALUE;

    EntityManager em = createEM();
    //        EntityTransaction tx = em.getTransaction();
    //        tx.begin();

    List<Product> products = em.createNamedQuery("priceList", Product.class).setParameter("cat", cat1)
            .setParameter("prod", prod1).setParameter("from", from1).setParameter("to", to1).getResultList();

    //        tx.commit();
    em.close();
    logger.log(Level.FINER, "get prods result size {0}", products.size());
    return products;
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryReferenceCheckImpl.java

@Override
public void perform(long now) {
    logger.debug("{}", name);
    EntityManager em = datastore.getEntityManager();
    try {/*from w  w w . j  a  va2  s  . c  om*/
        em.getTransaction().begin();
        TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllPublic",
                VirtualCollection.class);
        q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
        for (VirtualCollection vc : q.getResultList()) {
            checkValidityOfReferences(vc);
        }
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        logger.error("unexpected error while doing " + name, e);
    } finally {
        datastore.closeEntityManager();
    }
}

From source file:com.headissue.pigeon.survey.answer.AnswerHandler.java

UserMap loadUserMap(Survey _survey, int _mapId, EntityManager _manager) {
    TypedQuery<UserMap> q = _manager.createNamedQuery("answerUserMan.findBySurveyAndId", UserMap.class);
    q.setParameter("survey", _survey);
    q.setParameter("mapId", _mapId);
    return JPAUtils.getSingleResult(q);
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void purgeDeletedCollections(EntityManager em, final Date nowDatePurge) {
    em.getTransaction().begin();/*from w  w  w .j  a va  2  s . c o  m*/
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByState",
            VirtualCollection.class);
    q.setParameter("state", VirtualCollection.State.DELETED);
    q.setParameter("date", nowDatePurge);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        vc.setState(VirtualCollection.State.DEAD);
        em.remove(vc);
        logger.debug("purged virtual collection (id={})", vc.getId());
    }
    em.getTransaction().commit();
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void handleCollectionsInError(EntityManager em, final Date nowDateError) {
    em.getTransaction().begin();/*from   w  w w.ja  va2s.  c  o m*/
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByState",
            VirtualCollection.class);
    q.setParameter("state", VirtualCollection.State.ERROR);
    q.setParameter("date", nowDateError);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        VirtualCollection.State currentState = vc.getState();
        logger.info("Found [{}] in error state.", vc.getName(), currentState);
        //TODO: handle virtual collections in error state
    }
    em.getTransaction().commit();
}