Example usage for javax.persistence TypedQuery getParameter

List of usage examples for javax.persistence TypedQuery getParameter

Introduction

In this page you can find the example usage for javax.persistence TypedQuery getParameter.

Prototype

Parameter<?> getParameter(String name);

Source Link

Document

Get the parameter object corresponding to the declared parameter of the given name.

Usage

From source file:org.springframework.data.jpa.repository.support.SimpleJpaRepository.java

public boolean exists(ID id) {

    Assert.notNull(id, "The given id must not be null!");

    if (entityInformation.getIdAttribute() == null) {
        return findOne(id) != null;
    }/* ww  w.j  a v a 2s . c  om*/

    String placeholder = provider.getCountQueryPlaceholder();
    String entityName = entityInformation.getEntityName();
    Iterable<String> idAttributeNames = entityInformation.getIdAttributeNames();
    String existsQuery = QueryUtils.getExistsQueryString(entityName, placeholder, idAttributeNames);

    TypedQuery<Long> query = em.createQuery(existsQuery, Long.class);

    if (!entityInformation.hasCompositeId()) {
        query.setParameter(idAttributeNames.iterator().next(), id);
        return query.getSingleResult() == 1L;
    }

    for (String idAttributeName : idAttributeNames) {

        Object idAttributeValue = entityInformation.getCompositeIdAttributeValue(id, idAttributeName);

        boolean complexIdParameterValueDiscovered = idAttributeValue != null && !query
                .getParameter(idAttributeName).getParameterType().isAssignableFrom(idAttributeValue.getClass());

        if (complexIdParameterValueDiscovered) {

            // fall-back to findOne(id) which does the proper mapping for the parameter.
            return findOne(id) != null;
        }

        query.setParameter(idAttributeName, idAttributeValue);
    }

    return query.getSingleResult() == 1L;
}