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:it.infn.ct.futuregateway.apiserver.v1.ApplicationCollectionService.java

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

From source file:com.epam.training.taranovski.web.project.repository.implementation.BasicSkillRepositoryImplementation.java

@Override
public List<BasicSkill> getSkillsNotInEmployee(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<BasicSkill> list = new LinkedList<>();
    try {/*from w w w .  j  a  v  a2 s.  co m*/
        em.getTransaction().begin();

        TypedQuery<BasicSkill> query = em.createNamedQuery("BasicSkill.findSkillsNotInEmployee",
                BasicSkill.class);
        query.setParameter("employee", employee);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.BasicSkillRepositoryImplementation.java

@Override
public List<BasicSkill> getSkillsNotInVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<BasicSkill> list = new LinkedList<>();
    try {/* www.  ja v a  2  s.  com*/
        em.getTransaction().begin();

        TypedQuery<BasicSkill> query = em.createNamedQuery("BasicSkill.findSkillsNotInVacancy",
                BasicSkill.class);
        query.setParameter("vacancy", vacancy);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

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

public CategoryData findCategory(String category) {
    EntityManager em = null;
    try {//ww w. j a  v  a 2s  .  c o  m
        em = entityService.get();
        TypedQuery<Category> q = em.createNamedQuery("findCategory", Category.class);
        q.setParameter("category", category);
        Category cat = getSingleResult(q);
        if (cat == null) {
            return new CategoryView();
        } else {
            return new CategoryView(cat.getId(), cat.getTitle());
        }
    } finally {
        EntityService.close(em);
    }
}

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

public List<NoteData> getNoteList() {
    EntityManager em = null;
    try {//from w w w. j a v a  2 s  . c o m
        em = entityService.get();
        TypedQuery<Note> q = em.createNamedQuery("selectAll", Note.class);
        List<Note> notes = getResultList(q);
        if (notes == null || notes.isEmpty()) {
            return Collections.emptyList();
        }
        int initSize = notes.size();
        List<NoteData> noteList = new ArrayList<NoteData>(initSize);
        for (Note note : notes) {
            NoteView noteData = beanCopy.copy(note, NoteView.class, noteProperties);
            if (note.getCategory() != null) {
                noteData.setCategory(note.getCategory().getTitle());
            }
            noteList.add(noteData);
        }
        return noteList;
    } finally {
        EntityService.close(em);
    }
}

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

public List<CategoryData> getCategoryList() {
    EntityManager em = null;
    try {//from  www.  j a  v a 2  s.  c om
        em = entityService.get();
        TypedQuery<Category> q = em.createNamedQuery("allCategories", Category.class);
        List<Category> list = getResultList(q);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        int initSize = list.size();
        List<CategoryData> categoryList = new ArrayList<CategoryData>(initSize);
        for (Category category : list) {
            CategoryView categoryData = beanCopy.copy(category, CategoryView.class, categoryProperties);
            categoryList.add(categoryData);
        }
        return categoryList;
    } finally {
        EntityService.close(em);
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserRepositoryImplementation.java

@Override
public boolean nameExistsInDB(String name) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean exists = false;

    try {/*  w  w  w. j a va2 s.c o  m*/
        em.getTransaction().begin();

        TypedQuery<User> query = em.createNamedQuery("User.findByLogin", User.class);
        query.setParameter("login", name);

        query.getSingleResult();

        em.getTransaction().commit();
        exists = true;
    } catch (NoResultException ex) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(ex);
        exists = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return exists;
}

From source file:org.dragoneronca.nlp.wol.domain.WolEntityIterator.java

@SuppressWarnings("unchecked")
private boolean retrieveResults() {
    if (ended) {/*from w ww .j  a v a 2  s.c  o  m*/
        return false;
    } else if (!results.isEmpty()) {
        return true;
    }

    WolDomainContext domainContext = WolDomainContext.getInstance();
    EntityManager entityManager = domainContext.getEntityManager();
    if (clear) {
        entityManager.clear();
    }
    TypedQuery<T> q = entityManager.createNamedQuery(name, tClass);

    if (parameters != null) {
        int i = 1;
        for (Object parameter : parameters) {
            q.setParameter(i++, parameter);
        }
    }
    q.setFirstResult(offset).setMaxResults(max);

    long startQueryTime = System.currentTimeMillis();
    if (logEnabled) {
        LOG.info("Starting query: " + name + "\t\tType: " + tClass.getSimpleName());
    }

    Runtime.getRuntime().gc();
    List<T> resultList = q.getResultList();

    if (!resultList.isEmpty()) {
        results.addAll(resultList);
        offset += results.size();

        long endQueryTime = System.currentTimeMillis();
        if (logEnabled) {
            LOG.info("Retrieved " + results.size() + " results in " + ((endQueryTime - startQueryTime) / 1000)
                    + " sec.");
        }

        return true;
    } else {
        ended = true;
        return false;
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployerRepositoryImplementation.java

@Override
public List<Vacancy> getActiveVacancys(Employer employer) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Vacancy> list = new LinkedList<>();
    try {// w  ww.ja v  a 2s . c  o  m
        em.getTransaction().begin();

        TypedQuery<Vacancy> query = em.createNamedQuery("Vacancy.findActiveByEmployer", Vacancy.class);
        query.setParameter("employer", employer);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);

    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.CheckDocumentRepositoryImplementation.java

@Override
public CheckDocument findByEmployee(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();

    CheckDocument checkDocument = null;//from w w  w  . j  av  a2s .c  om
    try {
        em.getTransaction().begin();

        TypedQuery<CheckDocument> query = em.createNamedQuery("CheckDocument.findByEmployee",
                CheckDocument.class);
        query.setParameter("employee", employee);
        checkDocument = query.getSingleResult();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return checkDocument;
}