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:elaborate.editor.backend.AnnotationMarkerScrubber.java

@SuppressWarnings("boxing")
public static void main(String[] args) {
    StopWatch sw = new StopWatch();
    sw.start();//from   w w  w. j a  v  a  2s .c o  m
    EntityManager entityManager = HibernateUtil.beginTransaction();
    TranscriptionService ts = TranscriptionService.instance();
    ts.setEntityManager(entityManager);
    try {
        List<Transcription> resultList = entityManager// .
                .createQuery("select t from Transcription t", Transcription.class)//
                .getResultList();
        int size = resultList.size();
        int n = 1;
        for (Transcription t : resultList) {
            Log.info("indexing transcription {} ({}/{} = {}%)",
                    new Object[] { t.getId(), n, size, percentage(n, size) });
            String bodyBefore = t.getBody();
            ts.cleanupAnnotations(t);
            String bodyAfter = t.getBody();
            if (!bodyAfter.equals(bodyBefore)) {
                ProjectEntry projectEntry = t.getProjectEntry();
                String projectname = projectEntry.getProject().getName();
                long entryId = projectEntry.getId();
                Log.info("url: http://test.elaborate.huygens.knaw.nl/projects/{}/entries/{}/transcriptions/{}",
                        projectname, entryId, t.getTextLayer());
                Log.info("body changed:\nbefore: {}\nafter:{}", bodyBefore, bodyAfter);
            }
            n++;
        }
    } finally {
        HibernateUtil.commitTransaction(entityManager);
    }
    sw.stop();
    Log.info("done in {}", convert(sw.getTime()));
}

From source file:fr.univrouen.poste.domain.LogMail.java

public static List<String> getAllMailTo() {
    EntityManager em = entityManager();
    TypedQuery<String> q = em.createQuery("select distinct(o.mailTo) FROM LogMail o ORDER BY o.mailTo",
            String.class);
    return q.getResultList();
}

From source file:org.kew.rmf.matchconf.Dictionary.java

public static TypedQuery<Dictionary> findDictionariesByNameEquals(String name) {
    if (name == null || name.length() == 0)
        throw new IllegalArgumentException("The name argument is required");
    EntityManager em = Dictionary.entityManager();
    TypedQuery<Dictionary> q = em.createQuery("SELECT o FROM Dictionary AS o WHERE o.name = :name",
            Dictionary.class);
    q.setParameter("name", name);
    return q;//  ww  w. j a va  2 s . c om
}

From source file:ar.edu.unlp.sedici.sedici2003.model.Personas.java

public static List<Personas> findPersonasesByApellidoYNombre(String apellido, String nombre, int start,
        int count) {
    if (apellido == null || apellido.length() == 0)
        apellido = "";
    if (nombre == null)
        nombre = "";
    if (start < 0)
        start = 0;/*from   w  w  w . ja  v  a 2  s. c om*/
    if (count <= 0)
        count = 20;

    apellido = Personas.convertirParaQuery(apellido);
    nombre = Personas.convertirParaQuery(nombre);

    String where = Personas.generateCondition(apellido, nombre);

    EntityManager em = Personas.entityManager();
    TypedQuery<Personas> q = em.createQuery(
            "SELECT o FROM Personas AS o " + where + " ORDER BY o.apellido, o.nombre ASC", Personas.class);
    if (apellido.length() != 0)
        q.setParameter("apellido", apellido);
    if (nombre.length() != 0)
        q.setParameter("nombre", nombre);
    q.setFirstResult(start);
    q.setMaxResults(count);
    return q.getResultList();

}

From source file:ar.edu.unlp.sedici.sedici2003.model.TesaurosTermino.java

/**
 * Genera el camino de terminos hasta llegar al antecesor del termino actual
 * @param separador//w w  w .  j av  a2 s. co  m
 * @return
 */
public static String getCamino(TesaurosTermino entity, String separador) {
    String listaIDs = "'";
    String idTermino = entity.getId();

    // Armamos los IDs de los antecesores a partir del id del termino actual
    while (idTermino.contains(".")) {
        idTermino = idTermino.substring(0, idTermino.lastIndexOf("."));
        listaIDs += idTermino;
        if (idTermino.contains("."))
            listaIDs += "','";
    }
    listaIDs += "'";

    // Armamos la query
    String sqlAntecesores = "SELECT terminos FROM TesaurosTermino AS terminos " + "WHERE terminos.id IN ("
            + listaIDs + ") " + "ORDER BY terminos.id ASC";

    // Ejecutamos la query
    EntityManager em = TesaurosTermino.entityManager();
    TypedQuery<TesaurosTermino> q = em.createQuery(sqlAntecesores, TesaurosTermino.class);
    List<TesaurosTermino> antecesores = q.getResultList();

    // Procesamos los resultados y armamos el camino final
    String[] terminos = new String[antecesores.size()];
    for (int i = 0; i < antecesores.size(); i++) {
        terminos[i] = antecesores.get(i).getNombreEs();
    }
    return StringUtils.join(terminos, separador);
}

From source file:ar.edu.unlp.sedici.sedici2003.model.JerarquiasTermino.java

/**
 * Genera el camino de terminos hasta llegar al antecesor del termino actual
 * @param separador//w  w w  .jav  a 2s  . c om
 * @return
 */
public static String getCamino(JerarquiasTermino entity, String separador) {
    String listaIDs = "'";
    String idTermino = entity.getId();

    // Armamos los IDs de los antecesores a partir del id del termino actual
    while (idTermino.contains(".")) {
        idTermino = idTermino.substring(0, idTermino.lastIndexOf("."));
        listaIDs += idTermino;
        if (idTermino.contains("."))
            listaIDs += "','";
    }
    listaIDs += "'";

    // Armamos la query
    String sqlAntecesores = "SELECT terminos FROM JerarquiasTermino AS terminos " + "WHERE terminos.id IN ("
            + listaIDs + ") " + "ORDER BY terminos.id ASC";

    // Ejecutamos la query
    EntityManager em = JerarquiasTermino.entityManager();
    TypedQuery<JerarquiasTermino> q = em.createQuery(sqlAntecesores, JerarquiasTermino.class);
    List<JerarquiasTermino> antecesores = q.getResultList();

    // Procesamos los resultados y armamos el camino final
    String[] terminos = new String[antecesores.size()];
    for (int i = 0; i < antecesores.size(); i++) {
        terminos[i] = antecesores.get(i).getNombreEs();
    }
    return StringUtils.join(terminos, separador);
}

From source file:ar.edu.unlp.sedici.sedici2003.model.TesaurosTermino.java

public static List<TesaurosTermino> findAll(String text, String[] parents, boolean includeChilds, int start,
        int count) {

    //if (text == null || text.length() == 0) throw new IllegalArgumentException("The text argument is required");
    if (parents == null || parents.length == 0)
        throw new IllegalArgumentException("The parents argument is required");

    if (start < 0)
        start = 0;//from w ww  . j a v  a2  s.  c o m
    if (count <= 0)
        count = 60;

    // Armamos el filtro de descendencia
    String parentFilter = "(";
    if (includeChilds) {
        for (String parentID : parents) {
            if (!parentID.endsWith("."))
                parentID += ".";
            parentFilter += " terminos.id LIKE '" + parentID + "%' OR";
        }
    } else {
        for (String parentID : parents) {
            parentFilter += " relaciones.id.idTermino1 = '" + parentID + "' OR";
        }
    }

    //Sacamos el ultimo OR
    parentFilter = parentFilter.substring(0, parentFilter.length() - 2) + ")";

    String sql = "SELECT terminos " + "FROM TesaurosTermino AS terminos, TesaurosRelaciones AS relaciones "
            + "WHERE terminos.id = relaciones.id.idTermino2 AND relaciones.id.tipoRelacion = 1 "
            + "AND LOWER(terminos.nombreEs) LIKE LOWER(:filtro) AND " + parentFilter;

    //Agrego el orden
    sql = sql + " ORDER BY terminos.nombreEs ASC";

    EntityManager em = TesaurosTermino.entityManager();
    TypedQuery<TesaurosTermino> q = em.createQuery(sql, TesaurosTermino.class);
    if (text != null || text.length() != 0) {
        q.setParameter("filtro", "%" + text.trim() + "%");

    }
    q.setFirstResult(start);
    q.setMaxResults(count);

    return q.getResultList();
}

From source file:ar.edu.unlp.sedici.sedici2003.model.JerarquiasTermino.java

public static List<JerarquiasTermino> findAll(String text, String[] parents, boolean includeChilds,
        boolean includeSelf, int start, int count) {

    if (text == null || text.length() == 0)
        throw new IllegalArgumentException("The text argument is required");
    if (parents == null || parents.length == 0)
        throw new IllegalArgumentException("The parents argument is required");

    if (start < 0)
        start = 0;/*  ww  w .ja va2s  .co  m*/
    if (count <= 0)
        count = 10;

    // Armamos el filtro de descendencia
    String parentFilter = "(";
    if (includeChilds) {
        for (String parentID : parents) {
            if (!parentID.endsWith("."))
                parentID += ".";
            parentFilter += " terminos.id LIKE '" + parentID + "%' OR";
        }
    } else {
        for (String parentID : parents) {
            parentFilter += " relaciones.id.idTermino1 = '" + parentID + "' OR";
        }
    }

    if (includeSelf) {
        for (String parentID : parents) {
            if (parentID.endsWith("."))
                parentID = parentID.substring(0, parentID.length() - 1);

            parentFilter += " terminos.id LIKE '" + parentID + "' OR";
        }
    }

    //Sacamos el ultimo OR
    parentFilter = parentFilter.substring(0, parentFilter.length() - 2) + ")";

    String sql = "SELECT terminos " + "FROM JerarquiasTermino AS terminos, JerarquiasRelaciones AS relaciones "
            + "WHERE terminos.id = relaciones.id.idTermino2 AND relaciones.tipoRelacion = 1 "
            + "AND LOWER(terminos.nombreEs) LIKE LOWER(:filtro) AND " + parentFilter;

    //Agrego el orden
    sql = sql + " ORDER BY terminos.nombreEs ASC";

    //  System.out.println(sql);
    EntityManager em = JerarquiasTermino.entityManager();
    TypedQuery<JerarquiasTermino> q = em.createQuery(sql, JerarquiasTermino.class);
    q.setParameter("filtro", "%" + text + "%");
    q.setFirstResult(start);
    q.setMaxResults(count);

    return q.getResultList();
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static int getHistoryAllCount(EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM History h", Long.class);
    return q.getSingleResult().intValue();
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static int getQueueAllCount(EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM JobInstance h", Long.class);
    return q.getSingleResult().intValue();
}