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:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void allocatePersistentIdentifiers(EntityManager em, final Date nowDateAlloc) {
    em.getTransaction().begin();/*from w  w w  .j av a2s .  c om*/
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByStates",
            VirtualCollection.class);
    List<VirtualCollection.State> states = new LinkedList<>();
    states.add(VirtualCollection.State.PUBLIC_PENDING);
    states.add(VirtualCollection.State.PUBLIC_FROZEN_PENDING);
    q.setParameter("states", states);
    q.setParameter("date", nowDateAlloc);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        allocatePersistentIdentifier(em, vc);
    }
    em.getTransaction().commit();
}

From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java

/**
 * Gets the allby query./*from   w ww .j a  va 2s . c  o  m*/
 * 
 * @param em
 *            the em
 * @param target
 *            the target
 * @return the allby query
 * @since 28/08/2014, 11:20:27 PM
 */
protected List<Entity> getAllbyQuery(EntityManager em, Class<Entity> target) {
    TypedQuery<Entity> query = em.createNamedQuery(target.getSimpleName() + ".findAll", target);
    return query.getResultList();
}

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

private void storeAnswer(Survey _survey, Question _question, UserMap map, UserAnswerValue _value,
        EntityManager _manager, Date _timestamp) {
    TypedQuery<Answer> q = _manager.createNamedQuery("answer.findAnswerBySurveyQuestionAndUserMap",
            Answer.class);
    q.setParameter("survey", _survey);
    q.setParameter("question", _question);
    q.setParameter("userMap", map);
    Answer _answer = JPAUtils.getSingleResult(q);
    if (_answer == null) {
        _answer = new Answer();
        _answer.setTimestamp(_timestamp);
        _answer.setSurvey(_survey);//from w ww  .ja  v  a 2  s  .  com
        _answer.setQuestion(_question);
        _answer.setUserMap(map);
    } else {
        _answer.getAnswerValues().clear();
        _answer.getAnswerTexts().clear();
    }
    AnswerTransform _transform = transformFactory.get(_question.getType().toString());
    if (_transform.transfer(_answer, _value)) {
        _manager.persist(_answer);
    } else {
        LogUtils.warn(log,
                "store answer is failed (surveyId=%s, questionId=%s, questionType=%s) user answer '%s'",
                _survey.getId(), _question.getId(), _question.getType(), _value);
    }
}

From source file:ro.allevo.fintpws.model.UserEntity.java

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {

    EntityManagerFactory configEntityManagerFactory = Persistence.createEntityManagerFactory("fintpCFG");
    EntityManager emc = configEntityManagerFactory.createEntityManager();

    TypedQuery<RoleEntity> query = emc.createNamedQuery("RoleEntity.findUserAuthorities", RoleEntity.class);
    List<RoleEntity> authorities = query.setParameter("userid", userid).getResultList();
    return authorities;
}

From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java

/**
 * Find last.//from w  w  w.  j a  va 2s . c  o m
 * 
 * @param dbAccess
 *            the db access
 * @param target
 *            the target
 * @param newRecord
 *            the new record
 * @return the entity
 * @since 28/08/2014, 11:20:27 PM
 */
@Transactional(propagation = Propagation.REQUIRED)
protected Entity findLast(EntityManager dbAccess, Class<Entity> target, Entity newRecord) {
    TypedQuery<Entity> query = dbAccess.createNamedQuery(newRecord.getNamedQueryId(), target);
    return query.getSingleResult();
}

From source file:org.eclipse.skalli.core.storage.jpa.JPAStorageComponent.java

public List<HistoryStorageItem> getHistory(String category, String id) throws StorageException {
    EntityManager em = getEntityManager();

    List<HistoryStorageItem> resultList;
    try {/*www. j av a 2  s  .co m*/
        TypedQuery<HistoryStorageItem> query = em.createNamedQuery("getItemsByCompositeKey",
                HistoryStorageItem.class);
        query.setParameter("category", category);
        query.setParameter("id", id);
        resultList = query.getResultList();
    } catch (Exception e) {
        throw new StorageException("Failed to retrieve historical data", e);
    } finally {
        em.close();
    }

    return resultList;
}

From source file:org.eclipse.skalli.core.storage.jpa.JPAStorageComponent.java

@Override
public List<String> keys(String category) throws StorageException {
    EntityManager em = getEntityManager();

    List<String> resultList = new ArrayList<String>();
    try {/*from  www  . jav a2  s.  c om*/
        TypedQuery<String> query = em.createNamedQuery("getIdsByCategory", String.class);
        query.setParameter("category", category);
        resultList = query.getResultList();
    } catch (Exception e) {
        throw new StorageException("Failed to retrieve IDs", e);
    } finally {
        em.close();
    }

    return resultList;
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public Date getLastInstanceTime(String feedName) throws ResultNotFoundException {
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_LATEST_INSTANCE_TIME, Date.class);
    q.setParameter("feedName", feedName);
    Date result = (Date) q.getSingleResult();
    entityManager.close();/*w  w  w. j  a va 2s . c o  m*/
    return result;
}

From source file:it.infn.ct.futuregateway.apiserver.v1.InfrastructureCollectionService.java

/**
 * Retrieve the application list./* www  . j  ava2  s  . c o m*/
 * The fields not requested for the list are cleaned.
 * @return The list of applications
 */
private List<Infrastructure> retrieveInfrastructureList() {
    List<Infrastructure> lstInfras;
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    try {
        et = em.getTransaction();
        et.begin();
        lstInfras = em.createNamedQuery("infrastructures.all", Infrastructure.class).getResultList();
        et.commit();
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to retrieve the infrastructure list");
        log.error(re);
        throw new RuntimeException("Impossible to access the " + "infrastructures list");
    } finally {
        em.close();
    }
    if (lstInfras != null) {
        for (Infrastructure in : lstInfras) {
            in.setDescription(null);
            in.setParameters(null);
        }
    }
    return lstInfras;
}

From source file:com.headissue.pigeon.admin.AdminSurveyHandler.java

public List<SurveyOverviewValue> getAllSurveys() {
    EntityManager _manager = factory.createEntityManager();
    try {/*from   www .  j a  v a 2 s .c om*/
        TypedQuery<SurveyOverviewValue> q = _manager.createNamedQuery("survey.allSurveyOverview",
                SurveyOverviewValue.class);
        q.setParameter("status", SurveyStatus.ENABLED);
        return JPAUtils.getResultList(q);
    } finally {
        _manager.close();
    }
}