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:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public List<Vacancy> getBidsForEmployee(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Vacancy> list = new LinkedList<>();
    try {/*from ww  w  . j  av a 2s .  co m*/
        em.getTransaction().begin();

        TypedQuery<Vacancy> query = em.createNamedQuery("OfferBid.findVacancyBidsForEmployee", Vacancy.class);
        query.setParameter("employee", employee);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.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.OfferBidRepositoryImplementation.java

@Override
public List<Employee> getBidsForVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Employee> list = new LinkedList<>();
    try {//from  w ww.  j  ava 2  s .c  om
        em.getTransaction().begin();

        TypedQuery<Employee> query = em.createNamedQuery("OfferBid.findEmployeeBidsForVacancy", Employee.class);
        query.setParameter("vacancy", vacancy);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.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.OfferBidRepositoryImplementation.java

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

        TypedQuery<Employee> query = em.createNamedQuery("OfferBid.findEmployeeOffersForVacancy",
                Employee.class);
        query.setParameter("vacancy", vacancy);
        list = query.getResultList();

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

    return list;
}

From source file:in.bookmylab.jpa.JpaDAO.java

public List<StaticData> getStaticData() {
    EntityManager em = emf.createEntityManager();
    List<StaticData> data = null;
    try {/*www . j a  v  a2 s .c o m*/
        data = em.createNamedQuery("StaticData.findAll", StaticData.class).getResultList();
    } finally {
        em.clear();
        em.close();
    }
    return data;
}

From source file:in.bookmylab.jpa.JpaDAO.java

public UserProfile getProfile(User user) {
    EntityManager em = emf.createEntityManager();
    UserProfile p = null;//from www .j ava2s . com
    try {
        List<UserProfile> res = em.createNamedQuery("UserProfile.findByUser", UserProfile.class)
                .setParameter("user", user).getResultList();
        if (!res.isEmpty()) {
            p = res.get(0);
        }
    } finally {
        em.close();
    }
    return p;
}

From source file:org.opencastproject.themes.persistence.ThemesServiceDatabaseImpl.java

private List<Theme> getThemes() throws ThemesServiceDatabaseException {
    EntityManager em = null;
    try {/*www  . ja  v  a 2 s.  c  om*/
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        TypedQuery<ThemeDto> q = em.createNamedQuery("Themes.findByOrg", ThemeDto.class).setParameter("org",
                orgId);
        List<ThemeDto> themeDtos = q.getResultList();

        List<Theme> themes = new ArrayList<Theme>();
        for (ThemeDto themeDto : themeDtos) {
            themes.add(themeDto.toTheme(userDirectoryService));
        }
        return themes;
    } catch (Exception e) {
        logger.error("Could not get themes: {}", ExceptionUtils.getStackTrace(e));
        throw new ThemesServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:in.bookmylab.jpa.JpaDAO.java

public HashMap<String, Object> getDashboardData(boolean isManager, int userId) {
    EntityManager em = emf.createEntityManager();
    HashMap<String, Object> data = new HashMap<>();
    try {//from w  ww.ja  v  a2  s  . co  m
        if (isManager) {

            List<User> l = em.createNamedQuery("User.findUnverified", User.class).getResultList();
            data.put(Constants.JK_UNVERIFIED_USERS, l);

            List<ResourceBooking> l2 = em
                    .createNamedQuery("ResourceBooking.findUnfinished", ResourceBooking.class).getResultList();
            data.put(Constants.JK_PENDING_BOOKINGS, l2);

        } else {
            List<ResourceBooking> l2 = em
                    .createNamedQuery("ResourceBooking.findUnfinishedForUser", ResourceBooking.class)
                    .setParameter("userId", userId).getResultList();
            data.put(Constants.JK_PENDING_BOOKINGS, l2);

            // TODO: Add other lab bookings

        }
    } finally {
        em.close();
    }
    return data;
}

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

public Long storeNote(NoteData noteData) {
    EntityManager em = null;
    EntityTransaction tx = null;//from  www  . j a  v a2 s  . c  o m
    try {
        em = entityService.get();
        tx = em.getTransaction();
        tx.begin();
        // search for category
        String category = noteData.getCategory();
        if (StringUtils.isEmpty(category)) {
            LogUtils.warn(log, "note without category is not allow [%s]", noteData);
            return null;
        }
        TypedQuery<Category> q = em.createNamedQuery("findCategory", Category.class);
        q.setParameter("category", category);
        Category cat = getSingleResult(q);
        if (cat == null) {
            // a new category
            cat = new Category();
            cat.setTitle(category);
            em.persist(cat);
        }
        final Note note;
        if (NumberUtils.isEmpty(noteData.getId())) {
            // create a new note
            note = beanCopy.copy(noteData, Note.class, noteProperties);
            note.setCategory(cat);
            cat.getNotes().add(note);
        } else {
            // update an existed note
            note = em.find(Note.class, noteData.getId());
            beanCopy.copy(noteData, note, noteProperties);
            if (!NumberUtils.compare(note.getCategory().getId(), cat.getId())) {
                // move to other category
                note.getCategory().getNotes().remove(note);
                cat.getNotes().add(note);
                note.setCategory(cat);
            }
        }
        EntityService.commit(tx);
        return note.getId();
    } catch (Exception e) {
        EntityService.rollback(tx);
        return null;
    } finally {
        EntityService.close(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*from   w  w w.ja  v  a 2s .  c  o  m*/
 */
@Override
public <T> List<T> findByJpaNamedQuery(Class<T> resultClass, String namedQuery, List<Object> posParams) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        TypedQuery<T> query = em.createNamedQuery(namedQuery, resultClass);
        JpaUtil.bindQueryParams(query, posParams);
        return query.getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}//from w  w w . java  2 s  . c  o m
 */
@Override
public <T> T getScalarResultOfType(Class<T> resultClass, String namedQuery, List<Object> posParams) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        TypedQuery<T> query = em.createNamedQuery(namedQuery, resultClass);
        JpaUtil.bindQueryParams(query, posParams);
        return query.getSingleResult();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}