Example usage for javax.persistence EntityManager createQuery

List of usage examples for javax.persistence EntityManager createQuery

Introduction

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

Prototype

public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass);

Source Link

Document

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

Usage

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public List<Troop> getAllTroops() {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//from   w w  w  . jav a2 s.  c  o m
    List<Troop> troops = em.createQuery("SELECT t FROM Troop t", Troop.class).getResultList();
    em.getTransaction().commit();
    em.close();
    return troops;
}

From source file:io.coala.persist.Persistable.java

@Transactional
@SuppressWarnings("unchecked")
default Stream<DAO> findSync(final EntityManager em, final String query) {
    final Class<DAO> daoType = (Class<DAO>) TypeArguments.of(Persistable.class, getClass()).get(0);
    return em.createQuery(query, daoType).getResultList().stream();
}

From source file:com.soen.smbank.dao.ObjectDao.java

public ArrayList getAllObjects(Class<T> ClassName, String tableName) {
    EntityManager em = this.getEMF().createEntityManager();
    ArrayList entities = null;//from   w w  w. j a va 2  s  .  c o  m
    try {
        entities = (ArrayList) em.createQuery("SELECT tb FROM " + tableName + " tb ", ClassName)
                .getResultList();

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

From source file:com.tsystems.javaschool.logiweb.dao.jpa.TruckDaoJpa.java

/**
 * {@inheritDoc}/*from  www. j av  a2 s. com*/
 */
@Override
public Set<Truck> findByMinCapacityWhereStatusOkAndNotAssignedToOrder(float minCargoCapacity)
        throws DaoException {
    try {
        EntityManager em = getEntityManager();
        Class<Truck> truckEntityClass = getEntityClass();
        String classSimpleName = truckEntityClass.getSimpleName();

        Query query = em.createQuery("SELECT t FROM " + classSimpleName
                + " t WHERE status = :status AND assignedDeliveryOrder IS NULL AND"
                + "  cargoCapacity >= :capacity", truckEntityClass);
        query.setParameter("status", TruckStatus.OK);
        query.setParameter("capacity", minCargoCapacity);

        List<Truck> resultList = query.getResultList();
        return new HashSet<Truck>(resultList);
    } catch (Exception e) {
        LOG.warn(e);
        throw new DaoException(e);
    }
}

From source file:com.tsystems.javaschool.logiweb.dao.jpa.TruckDaoJpa.java

/**
 * {@inheritDoc}/*from   w  w w  .ja v  a  2 s .c  om*/
 */
@Override
public Truck findByLicensePlate(String licensePlate) throws DaoException {
    try {
        EntityManager em = getEntityManager();
        Class<Truck> truckEntityClass = getEntityClass();
        String classSimpleName = truckEntityClass.getSimpleName();

        Query query = em.createQuery("SELECT t FROM " + classSimpleName + " t WHERE licencePlate = :plate",
                truckEntityClass);
        query.setParameter("plate", licensePlate);

        List<Truck> resultList = query.getResultList();
        if (resultList.isEmpty()) {
            return null;
        } else {
            return resultList.get(0);
        }
    } catch (Exception e) {
        LOG.warn(e);
        throw new DaoException(e);
    }
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

public List<DataSource> getAllDataSources() {
    EntityManager em = getEntityManager();
    try {/*from w  w w .j  a  v a2 s. c o  m*/
        return em.createQuery("SELECT ds FROM DataSource ds", DataSource.class).getResultList();
    } finally {
        em.close();
    }
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

/**
 * //from w w w  .jav a 2 s  .  co  m
 * @return 
 */
public List<StatusTrail> getAllInprogressTasks() {
    EntityManager em = getEntityManager();
    try {
        return em.createQuery(
                "SELECT st FROM StatusTrail st WHERE  " + " st.status = 'in_progress' AND st.givenUp = 0",
                StatusTrail.class).getResultList();
    } finally {
        em.close();
    }
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

/**
 * Tasks whose status:not_done and givenUp:zero
 * @param ds/*from   w w w  .j  a v  a 2s. c o m*/
 * @return 
 */
public List<StatusTrail> getIncompleteTasks(DataSource ds) {
    EntityManager em = getEntityManager();
    try {
        return em.createQuery(
                "SELECT st FROM StatusTrail st WHERE st.dataSourceId = :dataSourceId "
                        + "AND st.status = 'not_done' AND st.givenUp = 0 ORDER BY st.id DESC",
                StatusTrail.class).setParameter("dataSourceId", ds.getId()).getResultList();
    } finally {
        em.close();
    }
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

/**
 * //from  www .ja va  2s . c  om
 * @return 
 */
public List<StatusTrail> getAllIncompleteTasks() {
    EntityManager em = getEntityManager();
    try {
        return em
                .createQuery(
                        "SELECT st FROM StatusTrail st WHERE  "
                                + " st.status = 'not_done' AND st.givenUp = 0 ORDER BY st.id DESC",
                        StatusTrail.class)
                .getResultList();
    } finally {
        em.close();
    }
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.PersistentDocumentStoreController.java

/**
 * Gets the size of a document store (number of documents contained therein).
 * @param em the {@link EntityManager} to use.
 * @param store the {@link PersistentDocumentStore} whose size is desired.
 * @return the {@link Long} count of documents.
 *///from  w  w w . j a  va  2  s .  com
public long getSize(EntityManager em, PersistentDocumentStore store) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(store, CannedMessages.NULL_ARGUMENT, "store");

    TypedQuery<byte[]> query = em
            .createQuery("SELECT doc.id FROM PersistentDocument doc " + "WHERE doc.store=:store", byte[].class);
    query.setParameter("store", store);

    return query.getResultList().size();
}