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 Query createQuery(CriteriaDelete deleteQuery);

Source Link

Document

Create an instance of Query for executing a criteria delete query.

Usage

From source file:de.iai.ilcd.model.dao.UserDao.java

public String getSalt(String userName) {
    EntityManager em = PersistenceUtil.getEntityManager();
    try {//from ww  w  .jav a  2  s  .c o m
        return (String) em.createQuery("select u.passwordHashSalt from User u where u.userName=:userName")
                .setParameter("userName", userName).getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:de.iai.ilcd.model.dao.UserDao.java

public Long getUsersCount(Organization org) {
    if (org == null) {
        return null;
    }//from w  ww  . jav  a  2 s  .c o  m
    EntityManager em = PersistenceUtil.getEntityManager();

    try {
        Query q = em.createQuery(
                "SELECT COUNT(DISTINCT u) FROM User u WHERE u.organization.id = :orgId ORDER BY u.userName");
        q.setParameter("orgId", org.getId());
        return (Long) q.getSingleResult();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaEmailService.java

@Override
public JpaEmailRecord getByAddress(String emailAddress) {
    final EntityManager entityManager = entityManagerFactory.createEntityManager();
    try {//from w w  w  .  j  a v  a2  s  .  co  m
        final Query query = entityManager
                .createQuery("select e from EmailRecord e where e.emailAddress = :emailAddress");
        query.setParameter("emailAddress", emailAddress);
        @SuppressWarnings("unchecked")
        final List<JpaEmailRecord> results = query.getResultList();
        return results.size() == 1 ? results.get(0) : null;
    } finally {
        entityManager.close();
    }
}

From source file:com.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaEmailService.java

@Override
public JpaEmailRecord getByName(String givenName, String familyName) {
    LOGGER.info("Looking up email info for {} {}", givenName, familyName);
    final EntityManager entityManager = entityManagerFactory.createEntityManager();
    try {//from  w  ww  .  j  a  v a 2s  .co  m
        final Query query = entityManager.createQuery(
                "select e from EmailRecord e where e.givenName = :givenName and e.familyName = :familyName");
        query.setParameter("givenName", givenName);
        query.setParameter("familyName", familyName);
        @SuppressWarnings("unchecked")
        final List<JpaEmailRecord> results = query.getResultList();
        return results.size() == 1 ? results.get(0) : null;
    } finally {
        entityManager.close();
    }
}

From source file:de.iai.ilcd.model.dao.UserDao.java

@SuppressWarnings("unchecked")
public List<User> getUsers(Organization org, Integer first, Integer pageSize) {
    if (org == null) {
        return null;
    }//from w w  w  .j  a  v a  2 s  .c o  m
    EntityManager em = PersistenceUtil.getEntityManager();

    try {
        Query q = em.createQuery(
                "SELECT DISTINCT u FROM User u WHERE u.organization.id = :orgId ORDER BY u.userName");
        q.setParameter("orgId", org.getId());
        if (first != null) {
            q.setFirstResult(first.intValue());
        }
        if (pageSize != null) {
            q.setMaxResults(pageSize.intValue());
        }
        return (List<User>) q.getResultList();
    } catch (Exception e) {
        return null;
    }
}

From source file:de.iai.ilcd.model.dao.UserDao.java

@SuppressWarnings("unchecked")
public List<User> getOrgLessUsers() {
    EntityManager em = PersistenceUtil.getEntityManager();

    try {//from   ww  w.j  a  v a2 s. c o m
        return (List<User>) em
                .createQuery("SELECT DISTINCT u FROM User u WHERE u.organization is NULL ORDER BY u.userName")
                .getResultList();
    } catch (Exception e) {
        return null;
    }
}

From source file:de.berlios.jhelpdesk.dao.jpa.TicketCategoryDAOJpa.java

@Transactional(readOnly = false) // TODO: to chyba nie powinno byc oddzielnie?
private void deleteChildNodes(final TicketCategory category) {
    final long nodeCount = getNodeCount();
    final long subtreeNodeCount = (category.getRight() - category.getLeft()) / 2;

    this.jpaTemplate.execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createQuery("DELETE FROM TicketCategory c WHERE c.left > ?1 AND c.right < ?2");
            q.setParameter(1, category.getLeft());
            q.setParameter(2, category.getRight());
            q.executeUpdate();/* w  ww .java  2 s  .  c om*/
            Query q2 = em.createQuery(
                    "UPDATE TicketCategory c SET c.left = c.left - ?1 " + "WHERE c.left > ?2 AND c.left < ?3");
            q2.setParameter(1, subtreeNodeCount * 2);
            q2.setParameter(2, category.getLeft());
            q2.setParameter(3, nodeCount * 2);
            q2.executeUpdate();

            Query q3 = em.createQuery("UPDATE TicketCategory c SET c.right = c.right - ?1 "
                    + "WHERE c.right >= ?2 AND c.right <= ?3");
            q3.setParameter(1, subtreeNodeCount * 2);
            q3.setParameter(2, category.getRight());
            q3.setParameter(3, nodeCount * 2);
            q3.executeUpdate();

            return null;
        }
    });
}

From source file:de.iai.ilcd.model.dao.UserDao.java

@SuppressWarnings("unchecked")
public List<User> getUsers(Organization org) {
    if (org == null) {
        return null;
    }/*from  w w w  .  j  ava2  s . com*/
    EntityManager em = PersistenceUtil.getEntityManager();

    try {
        return (List<User>) em
                .createQuery(
                        "SELECT DISTINCT u FROM User u WHERE u.organization.id = :orgId ORDER BY u.userName")
                .setParameter("orgId", org.getId()).getResultList();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaMembershipService.java

private JpaMembershipRecord getByName(EntityManager entityManager, String givenName, String familyName) {
    final Query query = entityManager.createQuery(
            "select m from MembershipRecord m where m.givenName = :givenName and m.familyName = :familyName");
    query.setParameter("givenName", givenName);
    query.setParameter("familyName", familyName);
    @SuppressWarnings("unchecked")
    final List<JpaMembershipRecord> results = query.getResultList();
    return results.size() == 1 ? results.get(0) : null;
}

From source file:de.iai.ilcd.model.dao.UserGroupDao.java

public UserGroup getGroup(String groupName) {
    EntityManager em = PersistenceUtil.getEntityManager();

    UserGroup group = null;//from w w w.  java 2  s. co  m
    try {
        group = (UserGroup) em.createQuery("select g from UserGroup g where g.groupName=:groupName")
                .setParameter("groupName", groupName).getSingleResult();
    } catch (NoResultException e) {
        // we do nothing here; just return null
    }
    return group;
}