Java Code Examples for javax.persistence.TypedQuery.setParameter()

The following are Jave code examples for showing how to use setParameter() of the javax.persistence.TypedQuery class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Mastering-Java-EE-Development-with-WildFly   File: CommonTestCase.java   Source Code and License Vote up 6 votes
@Test
public void testQuery() {
	TypedQuery<Forum> query = entityManager
			.createQuery("select f from Forum f left outer join fetch f.topics where f.id = :forumId", Forum.class);
	query.setParameter("forumId", 1);
	List<Forum> forumList = query.getResultList();
	assertEquals("simple query ok", 1, forumList.size());
	query = entityManager.createNamedQuery("findForumByIdFetchTopics", Forum.class);
	query.setParameter("forumId", 1);
	forumList = query.getResultList();
	assertEquals("named query ok", 1, forumList.size());
	String namedQuery = "my_runtime_named_query";
	entityManager.getEntityManagerFactory().addNamedQuery(namedQuery, query);
	query = entityManager.createNamedQuery(namedQuery, Forum.class);
	query.setParameter("forumId", 1);
	forumList = query.getResultList();
	assertEquals("named query ok", 1, forumList.size());
}
 
Example 2
Project: Pet-Supply-Store   File: OrderItemRepository.java   Source Code and License Vote up 6 votes
/**
 * Gets all order items for the given productId.
 * @param productId The id of the product ordered.
 * @param start The index of the first orderItem to return. Negative value to start at the beginning.
 * @param limit The maximum number of orderItem to return. Negative value to return all.
 * @return List of order items with the specified product.
 */
public List<PersistenceOrderItem> getAllEntitiesWithProduct(long productId, int start, int limit) {
	List<PersistenceOrderItem> entities = null;
	EntityManager em = getEM();
    try {
        em.getTransaction().begin();
        PersistenceProduct prod = em.find(PersistenceProduct.class, productId);
        if (prod != null) {
        	TypedQuery<PersistenceOrderItem> allMatchesQuery =
        			em.createQuery("SELECT u FROM " + getEntityClass().getName()
        					+ " u WHERE u.product = :prod", getEntityClass());
        	allMatchesQuery.setParameter("prod", prod);
    		entities = resultsWithStartAndLimit(em, allMatchesQuery, start, limit);
        }
        em.getTransaction().commit();
    } finally {
        em.close();
    }
	if (entities == null) {
		return new ArrayList<PersistenceOrderItem>();
	}
	return entities;
}
 
Example 3
Project: ait-platform   File: AitEjbUserStorageProvider.java   Source Code and License Vote up 6 votes
@Override
public List<UserModel> searchForUser(String search, RealmModel realm, int firstResult, int maxResults) {
	TypedQuery<UserEntity> query = em.createNamedQuery("searchForUser", UserEntity.class);
	query.setParameter("search", "%" + search.toLowerCase() + "%");
	if (firstResult != -1) {
		query.setFirstResult(firstResult);
	}
	if (maxResults != -1) {
		query.setMaxResults(maxResults);
	}
	List<UserEntity> results = query.getResultList();
	List<UserModel> users = new LinkedList<>();
	for (UserEntity entity : results)
		users.add(new UserAdapter(session, realm, model, entity));
	return users;
}
 
Example 4
Project: BecomeJavaHero   File: BookDAOImpl.java   Source Code and License Vote up 6 votes
public List<BookEntity> findById(int id) {
	EntityManager em = connection.createEntityManager();
	List<BookEntity> result;
	
	try {
		em.getTransaction().begin();
		TypedQuery<BookEntity> query = em.createQuery("Select b from BookEntity b where b.id = :id", BookEntity.class);
		query.setParameter("id", id);
		result = query.getResultList();
		em.getTransaction().commit();
	} finally {
		if (em.getTransaction().isActive()) {
			em.getTransaction().rollback();
		}
		em.close();
	}
	
	return result;
}
 
Example 5
Project: oscm   File: AuditLogDao.java   Source Code and License Vote up 6 votes
/**
 * load auditLogs by operationIds and date range, if operationIds is null or
 * empty, load auditLogs only by date range
 * 
 * @param operationIds
 * @param startTime
 * @param endTime
 * @return
 */
public List<AuditLog> loadAuditLogs(List<String> operationIds,
        long startTime, long endTime) {
    TypedQuery<AuditLog> query;
    if (operationIds == null || operationIds.isEmpty()) {
        query = em.createNamedQuery("AuditLog.findByDateRange",
                AuditLog.class);
    } else {
        query = em.createNamedQuery("AuditLog.findByOperationAndDateRange",
                AuditLog.class);
        query.setParameter("operationIds", operationIds);
    }
    query.setParameter("startTime", Long.valueOf(startTime));
    query.setParameter("endTime", Long.valueOf(endTime));
    return query.getResultList();
}
 
Example 6
Project: aws-photosharing-example   File: ShareFacadeTest.java   Source Code and License Vote up 6 votes
private Role createRole() {
    Role role = new Role();
    role.setRole(ADMINISTRATOR);

    TypedQuery<Role> query =_em.createQuery("select r from Role r where r.role=:roleName", Role.class);

    query.setParameter("roleName", ADMINISTRATOR);
    Role tmpRole = null;

    try {
        tmpRole = query.getSingleResult();
    }

    catch (javax.persistence.NoResultException exc) {
        exc.printStackTrace();
    }

    if (tmpRole != null)
        role = tmpRole;

    return role;
}
 
Example 7
Project: aws-photosharing-example   File: ContentFacadeTest.java   Source Code and License Vote up 6 votes
@AfterClass
public void cleanUp() {

    // Delete S3 bucket
    DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(ContentHelper.getInstance().getConfiguredBucketName());
    s3Client.deleteBucket(deleteBucketRequest);

    EntityManager _em = Persistence.createEntityManager();
    TypedQuery<User> query =_em.createQuery("select u from User u where u.userName=:userName", User.class);

    query.setParameter("userName", _user.getUserName());
    _user = query.getSingleResult();

    query.setParameter("userName", sharedUser.getUserName());
    sharedUser = query.getSingleResult();

    _em.getTransaction().begin();
    _em.remove(_user);
    _em.remove(sharedUser);
    _em.getTransaction().commit();
}
 
Example 8
Project: esup-sgc   File: CardActionMessage.java   Source Code and License Vote up 5 votes
public static TypedQuery<CardActionMessage> findCardActionMessagesByEtatFinal(Etat etatFinal) {
    if (etatFinal == null) throw new IllegalArgumentException("The etatFinal argument is required");
    EntityManager em = CardActionMessage.entityManager();
    TypedQuery<CardActionMessage> q = em.createQuery("SELECT o FROM CardActionMessage AS o WHERE o.etatFinal = :etatFinal", CardActionMessage.class);
    q.setParameter("etatFinal", etatFinal);
    return q;
}
 
Example 9
Project: mid-tier   File: VirtualAccountArchivist.java   Source Code and License Vote up 5 votes
/**
 * Find account by names. Note this will throw {@link NoResultException} which will roll back the
 * transaction if the account is not found.
 */
@LogDuration(limit = 50)
public VirtualAccount getAccountByAccountNumber(Long vaNumber) {
    TypedQuery<VirtualAccount> q = em.createQuery("select va from VirtualAccount va where va.vaNumber=:number",
            VirtualAccount.class);
    q.setParameter("number", vaNumber);
    return q.getSingleResult();
}
 
Example 10
Project: OperatieBRP   File: AfnemerindicatieJpaRepositoryImpl.java   Source Code and License Vote up 5 votes
@Override
public List<PersoonAfnemerindicatie> haalAfnemerindicatiesOp(final long persoonId) {
    final String query = "SELECT pc FROM PersoonAfnemerindicatie pc WHERE pc.persoon.id = :persoonIdIndicatie";
    final TypedQuery<PersoonAfnemerindicatie> typedQuery =
            entityManager.createQuery(query, PersoonAfnemerindicatie.class);
    typedQuery.setParameter("persoonIdIndicatie", persoonId);
    return typedQuery.getResultList();
}
 
Example 11
Project: soapbox-race-core   File: EventDataDAO.java   Source Code and License Vote up 5 votes
public EventDataEntity findByPersonaAndEventSessionId(Long personaId, Long eventSessionId) {
	TypedQuery<EventDataEntity> query = entityManager.createNamedQuery("EventDataEntity.findByPersonaAndEventSessionId", EventDataEntity.class);
	query.setParameter("personaId", personaId);
	query.setParameter("eventSessionId", eventSessionId);
	
	List<EventDataEntity> resultList = query.getResultList();
	return !resultList.isEmpty() ? resultList.get(0) : null;
}
 
Example 12
Project: soapbox-race-core   File: PromoCodeDAO.java   Source Code and License Vote up 5 votes
public PromoCodeEntity findByUser(UserEntity userEntity) {
	TypedQuery<PromoCodeEntity> query = entityManager.createNamedQuery("PromoCodeEntity.findByUser", PromoCodeEntity.class);
	query.setParameter("user", userEntity);
	
	List<PromoCodeEntity> resultList = query.getResultList();
	return !resultList.isEmpty() ? resultList.get(0) : null;
}
 
Example 13
Project: optashift-employee-rostering   File: ShiftRestServiceImpl.java   Source Code and License Vote up 5 votes
@Override
public ShiftTemplate getTemplate(Integer tenantId) {
    TypedQuery<ShiftTemplate> q = entityManager.createNamedQuery("ShiftTemplate.get", ShiftTemplate.class);
    q.setParameter("tenantId", tenantId);
    List<ShiftTemplate> result = q.getResultList();
    if (result.isEmpty()) {
        return null;
    } else if (1 != result.size()) {
        throw new IllegalStateException("Each tenant can only have 1 template! Found " + result.size()
                + "templates!");
    } else {
        return result.get(0);
    }
}
 
Example 14
Project: OperatieBRP   File: Lo3FilterRubriekRepositoryImpl.java   Source Code and License Vote up 5 votes
@Override
public final List<String> haalLo3FilterRubriekenVoorDienstbundel(final Integer dienstbundelId) {
    final TypedQuery<String> query = entityManager.createQuery(
            "select rubrieken.lo3Rubriek.naam from DienstbundelLo3Rubriek rubrieken where rubrieken.dienstbundel.id = :dienstbundelId", String.class);
    query.setParameter("dienstbundelId", dienstbundelId);
    return query.getResultList();
}
 
Example 15
Project: spring-data-examples   File: AccountServiceImpl.java   Source Code and License Vote up 5 votes
@Override
public List<Account> findByCustomer(Customer customer) {

	TypedQuery<Account> query = em.createQuery("select a from Account a where a.customer = ?1", Account.class);
	query.setParameter(1, customer);

	return query.getResultList();
}
 
Example 16
Project: OperatieBRP   File: AdministratieveHandelingJpaRepository.java   Source Code and License Vote up 5 votes
@Override
public List<BRPActie> haalAdministratieveHandelingOp(final Long administratieveHandelingId) {
    final TypedQuery<BRPActie> typedQuery = em
            .createQuery("SELECT a FROM BRPActie a "
                            + "JOIN FETCH a.administratieveHandeling admhnd WHERE admhnd.id = a.administratieveHandeling.id AND a"
                            + ".administratieveHandeling.id = "
                            + ":administratieveHandelingId",
                    BRPActie.class);
    typedQuery.setParameter("administratieveHandelingId", administratieveHandelingId);
    return typedQuery.getResultList();
}
 
Example 17
Project: 112016.pizzeria-app   File: PizzaService.java   Source Code and License Vote up 4 votes
public Pizza get(String code) {
	
	TypedQuery<Pizza> query = em.createQuery("SELECT p FROM Pizza p WHERE p.code =:code", Pizza.class);
	query.setParameter("code", code);
	return query.getSingleResult();
}
 
Example 18
Project: optashift-employee-rostering   File: ShiftRestServiceImpl.java   Source Code and License Vote up 4 votes
private List<Shift> getAllShifts(Integer tenantId) {
    TypedQuery<Shift> q = entityManager.createNamedQuery("Shift.findAll", Shift.class);
    q.setParameter("tenantId", tenantId);
    return q.getResultList();
}
 
Example 19
Project: mid-tier   File: CustomerArchivist.java   Source Code and License Vote up 4 votes
public Event getEvent(String category, String id) {
    TypedQuery<Event> q = em.createQuery("select e from Event e where e.category=:category and e.id=:sid", Event.class);
    q.setParameter("category", category);
    q.setParameter("sid", id);
    return q.getResultList().get(0);
}
 
Example 20
Project: soapbox-race-core   File: EventDataDAO.java   Source Code and License Vote up 4 votes
public List<EventDataEntity> getRacers(Long eventSessionId) {
	TypedQuery<EventDataEntity> query = entityManager.createNamedQuery("EventDataEntity.getRacers", EventDataEntity.class);
	query.setParameter("eventSessionId", eventSessionId);
	return query.getResultList();
}