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:com.impetus.kwitter.service.TwitterService.java

@Override
public List<User> getFollowers(String userId) {
    Query q = em.createQuery("select u from User u where u.userId =:userId");
    q.setParameter("userId", userId);
    List<User> users = q.getResultList();
    if (users == null || users.isEmpty()) {
        return null;
    }/*from  w  ww  .j a v  a 2s . co  m*/
    return users.get(0).getFollowers();
}

From source file:com.rambird.repository.jpa.JpaOwnerRepositoryImpl.java

/**
 * Important: in the current version of this method, we load Owners with all their Pets and Visits while 
 * we do not need Visits at all and we only need one property from the Pet objects (the 'name' property).
 * There are some ways to improve it such as:
 * - creating a Ligtweight class (example here: https://community.jboss.org/wiki/LightweightClass)
 * - Turning on lazy-loading and using {@link OpenSessionInViewFilter}
 *//*from  w  w  w  .j a  v  a 2s . com*/
@SuppressWarnings("unchecked")
public Collection<Owner> findByLastName(String lastName) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em.createQuery(
            "SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE upper(owner.lastName) LIKE upper(:lastName)");
    query.setParameter("lastName", lastName + "%");
    return query.getResultList();
}

From source file:com.telefonica.euro_iaas.paasmanager.dao.impl.SecurityGroupDaoJpaImpl.java

public SecurityGroup loadWithRules(String name) throws EntityNotFoundException {

    Query query = getEntityManager()
            .createQuery("select p from SecurityGroup p left join " + " fetch p.rules where p.name = :name");
    query.setParameter("name", name);
    SecurityGroup securityGroup = null;//w ww. ja  va  2  s .  c  o  m
    try {
        securityGroup = (SecurityGroup) query.getResultList().get(0);
    } catch (Exception e) {
        String message = " No SecurityGroup found in the database with name: " + name + " Exception: "
                + e.getMessage();
        throw new EntityNotFoundException(SecurityGroup.class, "name", name);
    }
    return securityGroup;
}

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

@Override
public void updateClaim(String claimType, Claim claim) {
    Query query = null;
    query = em.createQuery("select c from Claim c where c.claimtype=:claimtype");
    query.setParameter("claimtype", claimType);

    //@SuppressWarnings("rawtypes")
    ClaimEntity claimEntity = (ClaimEntity) query.getSingleResult();

    domain2entity(claim, claimEntity);//from   w ww. j a  v a 2s .  c  o m

    LOG.debug("Claim '{}' added", claim.getClaimType());
    em.persist(claimEntity);
}

From source file:org.syncope.core.persistence.dao.impl.MembershipDAOImpl.java

@Override
public Membership find(final SyncopeUser user, final SyncopeRole role) {
    Query query = entityManager.createQuery(
            "SELECT e FROM Membership e " + "WHERE e.syncopeUser = :user AND e.syncopeRole = :role");
    query.setParameter("user", user);
    query.setParameter("role", role);

    Membership result = null;/*ww w . j a  va2 s .com*/

    try {
        result = (Membership) query.getSingleResult();
    } catch (NoResultException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No membership was found for user " + user + " and role " + role);
        }
    }

    return result;
}

From source file:com.impetus.kwitter.service.TwitterService.java

@Override
public List<Tweet> getAllTweets(String userId) {
    Query q = em.createQuery("select u from User u where u.userId =:userId");
    q.setParameter("userId", userId);
    List<User> users = q.getResultList();
    if (users == null || users.isEmpty()) {
        return null;
    } else {//from   ww  w. ja  va2  s  .c o  m
        return users.get(0).getTweets();
    }
}

From source file:com.pet.demo.repository.jpa.JpaAccountRepositoryImpl.java

public Account findByUsername(String username, String password) throws DataAccessException {
    Query query = this.em.createQuery(
            "SELECT account FROM Account account WHERE account.userName =:username AND account.password =:password ");
    query.setParameter("username", username);
    query.setParameter("password", password);
    Collection<Account> results = query.getResultList();
    if (results.size() == 1) {
        return results.iterator().next();
    }//from w  ww.ja v a  2 s  .c o m
    return null;
}

From source file:org.syncope.core.persistence.dao.impl.DerSchemaDAOImpl.java

@Override
public <T extends AbstractDerAttr> List<T> getAttributes(final AbstractDerSchema derivedSchema,
        final Class<T> reference) {

    Query query = entityManager.createQuery(
            "SELECT e FROM " + reference.getSimpleName() + " e" + " WHERE e.derivedSchema=:schema");

    query.setParameter("schema", derivedSchema);

    return query.getResultList();
}

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

@Override
public User findUserByName(String name) {
    Query query = entityManager.createNamedQuery("user:byName");
    query.setParameter("name", name);
    try {//from w w  w .  j a v  a 2 s .  c om
        return (User) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

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

@Override
public Role findRoleByName(String name) {
    Query query = entityManager.createNamedQuery("role:byName");
    query.setParameter("name", name);
    try {/*from   w  w  w . j  a  v  a2 s.  c  om*/
        return (Role) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}