Example usage for javax.persistence.criteria Join fetch

List of usage examples for javax.persistence.criteria Join fetch

Introduction

In this page you can find the example usage for javax.persistence.criteria Join fetch.

Prototype

<Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a fetch join to the specified single-valued attribute using an inner join.

Usage

From source file:org.wallride.service.PostService.java

/**
 *
 * @param language/* w ww .  j  a  v a  2s. c  o m*/
 * @param type
 * @return
 * @see PostService#updatePopularPosts(BlogLanguage, PopularPost.Type, int)
 */
@Cacheable(value = WallRideCacheConfiguration.POPULAR_POST_CACHE, key = "'list.type.' + #language + '.' + #type")
public SortedSet<PopularPost> getPopularPosts(String language, PopularPost.Type type) {
    Specification<PopularPost> spec = (root, query, cb) -> {
        Join<PopularPost, Post> post = (Join<PopularPost, Post>) root.fetch(PopularPost_.post);
        post.fetch(Post_.cover);
        post.fetch(Post_.author);

        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(root.get(PopularPost_.language), language));
        predicates.add(cb.equal(root.get(PopularPost_.type), type));
        predicates.add(cb.equal(post.get(Post_.status), Post.Status.PUBLISHED));
        return cb.and(predicates.toArray(new Predicate[0]));
    };
    return popularPostRepository.findAll(spec);
}