Search in sources :

Example 1 with EmptyResultException

use of io.micronaut.data.exceptions.EmptyResultException in project micronaut-data by micronaut-projects.

the class FindOneSpecificationInterceptor method intercept.

@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
    Specification specification = getSpecification(context);
    final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
    final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    final CriteriaQuery<Object> query = criteriaBuilder.createQuery((Class<Object>) getRequiredRootEntity(context));
    final Root<Object> root = query.from((Class<Object>) getRequiredRootEntity(context));
    final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
    if (predicate != null) {
        query.where(predicate);
    }
    query.select(root);
    final TypedQuery<?> typedQuery = entityManager.createQuery(query);
    try {
        final Object result = typedQuery.getSingleResult();
        final ReturnType<?> rt = context.getReturnType();
        final Class<?> returnType = rt.getType();
        if (returnType.isInstance(result)) {
            return result;
        } else {
            return operations.getConversionService().convertRequired(result, rt.asArgument());
        }
    } catch (NoResultException e) {
        if (context.isNullable()) {
            return null;
        } else {
            throw new EmptyResultException();
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) EmptyResultException(io.micronaut.data.exceptions.EmptyResultException) Specification(io.micronaut.data.jpa.repository.criteria.Specification) NoResultException(javax.persistence.NoResultException) Predicate(javax.persistence.criteria.Predicate)

Example 2 with EmptyResultException

use of io.micronaut.data.exceptions.EmptyResultException in project micronaut-data by micronaut-projects.

the class H2BookRepository method findByName.

@Transactional
public Author findByName(String name) {
    return jdbcOperations.prepareStatement("SELECT author_.id,author_.name,author_.nick_name,author_books_.id AS _books_id,author_books_.author_id AS _books_author_id,author_books_.title AS _books_title,author_books_.total_pages AS _books_total_pages,author_books_.publisher_id AS _books_publisher_id,author_books_.last_updated AS _books_last_updated FROM author AS author_ INNER JOIN book author_books_ ON author_.id=author_books_.author_id WHERE (author_.name = ?)", statement -> {
        statement.setString(1, name);
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            Author author = jdbcOperations.readEntity(resultSet, Author.class);
            Set<Book> books = new HashSet<>();
            do {
                books.add(jdbcOperations.readEntity("_books_", resultSet, Book.class));
            } while (resultSet.next());
            author.setBooks(books);
            return author;
        }
        throw new EmptyResultException();
    });
}
Also used : EmptyResultException(io.micronaut.data.exceptions.EmptyResultException) Book(io.micronaut.data.tck.entities.Book) ResultSet(java.sql.ResultSet) Author(io.micronaut.data.tck.entities.Author) HashSet(java.util.HashSet) Transactional(javax.transaction.Transactional)

Aggregations

EmptyResultException (io.micronaut.data.exceptions.EmptyResultException)2 Specification (io.micronaut.data.jpa.repository.criteria.Specification)1 Author (io.micronaut.data.tck.entities.Author)1 Book (io.micronaut.data.tck.entities.Book)1 ResultSet (java.sql.ResultSet)1 HashSet (java.util.HashSet)1 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 Transactional (javax.transaction.Transactional)1