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:org.kew.rmf.matchconf.Configuration.java

public static List<Configuration> findDedupConfigEntries(int firstResult, int maxResults) {
    EntityManager em = Configuration.entityManager();
    TypedQuery<Configuration> q = em.createQuery(
            "SELECT o FROM Configuration AS o WHERE o.authorityFileName = :authorityFileName",
            Configuration.class);
    q.setParameter("authorityFileName", "");
    return q.setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}

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

public static List<Configuration> findMatchConfigEntries(int firstResult, int maxResults) {
    EntityManager em = Configuration.entityManager();
    TypedQuery<Configuration> q = em.createQuery(
            "SELECT o FROM Configuration AS o WHERE o.authorityFileName != :authorityFileName",
            Configuration.class);
    q.setParameter("authorityFileName", "");
    return q.setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}

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

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

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

public static TypedQuery<Configuration> findMatchConfigsByNameEquals(String name) {
    if (name == null || name.length() == 0)
        throw new IllegalArgumentException("The name argument is required");
    EntityManager em = Configuration.entityManager();
    TypedQuery<Configuration> q = em.createQuery(
            "SELECT o FROM Configuration AS o WHERE o.name = :name AND o.authorityFileName != :authorityFileName",
            Configuration.class);
    q.setParameter("name", name);
    q.setParameter("authorityFileName", "");
    return q;//from  w ww . j ava 2s .co m
}

From source file:io.coala.bind.persist.LocalIdDao.java

public static LocalIdDao find(final EntityManager em, final LocalId id) {
    final Comparable<?> value = Objects.requireNonNull(id.unwrap());
    final LocalIdDao parentRef = id.parentRef().parentRef() == null ? null : find(em, id.parentRef());
    final UUID context = Objects.requireNonNull(id.contextRef());
    try {/*from   w  w w  .  j  ava 2s  .  c o  m*/
        return em
                .createQuery("SELECT d FROM " + ENTITY_NAME + " d WHERE d.value=?1 AND d.parentRef=?2"
                        + " AND d.contextRef=?3", LocalIdDao.class)
                .setParameter(1, value).setParameter(2, parentRef).setParameter(3, context).getSingleResult();
    } catch (final NoResultException ignore) {
        return null;
    }
}

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

public static TypedQuery<PosteCandidature> findPosteCandidaturesByCandidatAndByDateEndCandidatGreaterThan(
        User candidat, Date date) {
    EntityManager em = entityManager();
    String jpaQuery = "SELECT o FROM PosteCandidature AS o WHERE o.candidat = :candidat and o.poste.dateEndSignupCandidat > :date";
    TypedQuery<PosteCandidature> q = em.createQuery(jpaQuery, PosteCandidature.class);
    q.setParameter("candidat", candidat);
    q.setParameter("date", date);
    return q;// w  w w  .ja v a  2s.c  o  m
}

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

public static TypedQuery<PosteCandidature> findPosteCandidaturesByCandidatAndByDateEndCandidatGreaterThanAndNoAuditionnableOrByDateEndCandidatAuditionnableGreaterThanAndAuditionnable(
        User candidat, Date date) {
    EntityManager em = entityManager();
    String jpaQuery = "SELECT o FROM PosteCandidature AS o WHERE o.candidat = :candidat and (o.poste.dateEndSignupCandidat > :date and o.auditionnable = FALSE or o.poste.dateEndCandidatAuditionnable > :date and o.auditionnable = TRUE)";
    TypedQuery<PosteCandidature> q = em.createQuery(jpaQuery, PosteCandidature.class);
    q.setParameter("candidat", candidat);
    q.setParameter("date", date);
    return q;/*w w w . ja v  a2s. co m*/
}

From source file:com.enioka.jqm.tools.Main.java

private static void user(String[] options) {
    if (options.length < 3) {
        throw new IllegalArgumentException(
                "-U option requires one login, one password and at least one role (in this order)");
    }/*from   w  w w.ja  v  a  2  s . co  m*/

    EntityManager em = null;
    try {
        em = Helpers.getNewEm();

        RRole[] roles = new RRole[options.length - 2];
        for (int i = 2; i < options.length; i++) {
            try {
                roles[i - 2] = em.createQuery("SELECT r FROM RRole r WHERE r.name=:l", RRole.class)
                        .setParameter("l", options[i]).getSingleResult();
            } catch (NoResultException ex) {
                throw new IllegalArgumentException("Role " + options[i] + " does not exist");
            }
        }

        em.getTransaction().begin();
        RUser u = Helpers.createUserIfMissing(em, options[0], "created through CLI", roles);
        u.setPassword(options[1]);
        Helpers.encodePassword(u);
        em.getTransaction().commit();
    } finally {
        Helpers.closeQuietly(em);
    }
}

From source file:com.enioka.jqm.tools.Helpers.java

/**
 * Checks if a parameter exists. If it exists, it is left untouched. If it doesn't, it is created. Only works for parameters which key
 * is unique. Must be called from within an open JPA transaction.
 *//*  w  w w  . j a v a2s . c  o  m*/
static void initSingleParam(String key, String initValue, EntityManager em) {
    try {
        em.createQuery("SELECT n from GlobalParameter n WHERE n.key = :key", GlobalParameter.class)
                .setParameter("key", key).getSingleResult();
        return;
    } catch (NoResultException e) {
        GlobalParameter gp = new GlobalParameter();
        gp.setKey(key);
        gp.setValue(initValue);
        em.persist(gp);
    } catch (NonUniqueResultException e) {
        // It exists! Nothing to do...
    }
}

From source file:com.enioka.jqm.tools.Helpers.java

static JobDef findJobDef(String applicationName, EntityManager em) {
    try {//from w ww. ja v  a 2 s . c  o m
        return em.createQuery("SELECT j FROM JobDef j WHERE j.applicationName = :n", JobDef.class)
                .setParameter("n", applicationName).getSingleResult();
    } catch (NoResultException ex) {
        return null;
    }
}