Example usage for javax.persistence Query setParameter

List of usage examples for javax.persistence Query setParameter

Introduction

In this page you can find the example usage for javax.persistence Query setParameter.

Prototype

Query setParameter(int position, Object value);

Source Link

Document

Bind an argument value to a positional parameter.

Usage

From source file:es.ucm.fdi.dalgs.user.repository.UserRepository.java

public User findByEmail(String email) {
    Query query = em.createQuery("select s from User s where s.email=?1");
    query.setParameter(1, email);

    if (query.getResultList().isEmpty()) {
        //         logger.error("User not found by email " + email);
        return null;
    }//from  www  .  ja  va2s. c o  m
    return (User) query.getSingleResult();
}

From source file:com.expressui.sample.dao.PermissionDao.java

public List<Permission> findByRoleEntityTypeAndField(AbstractRole role, String entityType, String field) {
    Query query = getEntityManager().createQuery("SELECT p FROM Permission p WHERE p.role = :role"
            + " AND p.entityType = :entityType AND p.field = :field");
    query.setParameter("role", role);
    query.setParameter("entityType", entityType);
    query.setParameter("field", field);

    query.setFlushMode(FlushModeType.COMMIT);

    return query.getResultList();
}

From source file:it.proximacentauri.ienergy.da.dao.impl.ConfortDaoImpl.java

@Override
public Indicator findIndicator(String name) {
    Query query = em.createQuery("FROM Indicator WHERE name = :name");
    query.setParameter("name", name);

    // get the data from db
    @SuppressWarnings("unchecked")
    List<Indicator> list = query.getResultList();

    if (list.size() == 0)
        return null;

    return list.get(0);
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.TrustedIdpDAOJPAImpl.java

@Override
public void deleteTrustedIDP(String realm) {
    Query query = null;
    query = em.createQuery("select t from TrustedIDP t where t.realm=:realm");
    query.setParameter("realm", realm);

    //@SuppressWarnings("rawtypes")
    Object trustedIdpObj = query.getSingleResult();
    em.remove(trustedIdpObj);//from www.ja  va  2 s  . c  om

    LOG.debug("Trusted IDP '" + realm + "' deleted");
}

From source file:com.acmeair.jpa.service.FlightServiceImpl.java

@Transactional(propagation = Propagation.REQUIRED)
@Override//from  www  .  j  a v  a 2  s .co  m
public void storeAirportMapping(AirportCodeMapping mapping) {
    Query q = em.createQuery("SELECT obj FROM AirportCodeMapping obj where obj.id=?1 and obj.airportName=?2");
    q.setParameter(1, mapping.getAirportCode());
    q.setParameter(2, mapping.getAirportName());
    if (q.getResultList().isEmpty()) {
        try {
            em.persist(mapping);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.appdynamicspilot.persistence.CartPersistence.java

/**
 * Get the cart based on user/* w w  w.ja v  a2  s  .c o m*/
 *
 * @param userId
 * @return cart object
 */
public Cart getCartByUser(Long userId) {
    Query q = getEntityManager().createQuery("SELECT c FROM Cart c where c.user.id = :userid");
    q.setParameter("userid", userId);
    List<Cart> carts = q.getResultList();
    if ((carts == null) || (carts.size() == 0)) {
        return null;
    }
    return carts.get(0);
}

From source file:com.ushahidi.swiftriver.core.api.dao.impl.JpaAccountDao.java

public boolean deleteFollower(Account account, Account follower) {
    String sql = "DELETE FROM AccountFollower " + "WHERE account = :account " + "AND follower = :follower";

    Query query = em.createQuery(sql);
    query.setParameter("account", account);
    query.setParameter("follower", follower);

    return query.executeUpdate() == 1;
}

From source file:de.voolk.marbles.persistence.services.AuthentificationService.java

@Override
public User findUserByIdentKey(String identKey) {
    Query query = entityManager.createNamedQuery("user:byIdentKey");
    query.setParameter("identKey", identKey);
    try {/*from   ww w.  ja va2 s .c  o m*/
        return (User) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:es.ucm.fdi.dalgs.user.repository.UserRepository.java

public User findByUsername(String username) {
    Query query = em.createQuery("select s from User s where s.username=?1");
    query.setParameter(1, username);

    if (query.getResultList().isEmpty()) {
        logger.error("User not found by username " + username);
        return null;
    }/*from   w  w w  .j ava  2s .c om*/

    return (User) query.getSingleResult();
}

From source file:org.messic.server.datamodel.jpaimpl.DAOJPASong.java

@Override
@Transactional/*from   w  w w . j  a  v a2 s.  co m*/
public List<MDOSong> genericFind(String username, List<String> searches) {
    HashMap<Long, MDOSong> finalResult = new HashMap<Long, MDOSong>();
    for (int i = 0; i < searches.size(); i++) {
        String content = searches.get(i);

        String sql = "from MDOSong as a WHERE (a.owner.login = :userName) AND (";
        sql = sql + "(UPPER(a.name) LIKE :what) OR ";
        sql = sql + "(UPPER(a.album.name) LIKE :what) OR ";
        sql = sql + "(UPPER(a.album.genre.name) LIKE :what) OR ";
        try {
            Integer.parseInt(content);
            sql = sql + "(a.album.year LIKE :what) OR ";
        } catch (NumberFormatException nfe) {
        }
        sql = sql + "(UPPER(a.album.comments) LIKE :what) OR ";
        sql = sql + "(UPPER(a.album.author.name) LIKE :what)";
        sql = sql + ")";

        Query query = entityManager.createQuery(sql);
        query.setParameter("userName", username);
        query.setParameter("what", "%" + content.toUpperCase() + "%");

        @SuppressWarnings("unchecked")
        List<MDOSong> results = query.getResultList();
        for (MDOSong mdoSong : results) {
            finalResult.put(mdoSong.getSid(), mdoSong);
        }
    }

    ArrayList<MDOSong> result = new ArrayList<MDOSong>();
    Iterator<MDOSong> songsit = finalResult.values().iterator();
    while (songsit.hasNext()) {
        result.add(songsit.next());
    }
    return result;
}