0

In my Spring Boot/Data/JPA 2.1 application I have a following entity:

@Entity
@NamedEntityGraph(name = "graph.CardCategoryLevel", attributeNodes = { @NamedAttributeNode("cardCategory"), @NamedAttributeNode("level") })
@Table(name = "card_categories_levels")
public class CardCategoryLevel extends BaseEntity implements Serializable {

    @Id
    @SequenceGenerator(name = "card_categories_levels_id_seq", sequenceName = "card_categories_levels_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "card_categories_levels_id_seq")
    private Long id;

    @OneToOne
    @JoinColumn(name = "card_category_id")
    private CardCategory cardCategory;

    @OneToOne
    @JoinColumn(name = "level_id")
    private Level level;

    @Column(name = "card_drop_rate")
    private Float cardDropRate;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cardCategoryLevel")
    private List<Card> cards = new ArrayList<Card>();
....
}

and Spring Data CardCategoryLevelRepository:

@Repository
public interface CardCategoryLevelRepository extends JpaRepository<CardCategoryLevel, Long> {

    @Override
    @EntityGraph(value = "graph.CardCategoryLevel", type = EntityGraphType.FETCH)
    Page<CardCategoryLevel> findAll(Pageable pageable);

}

Based on CardCategoryLevelRepository.findAll(Pageable pageable) I can retrieve all CardCategoryLevel with pagination and sorting.

Right now I don't know how to apply filtering into this approach. For example I need to have possibility to filter CardCategoryLevel by CardCategory or/and by Level or/and by cardDropRate. How to inject filtering feature into CardCategoryLevelRepository.findAll(Pageable pageable) method or may be a new similar one ?

alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • you could either create a method name from which spring data would create the query(will result in huge method names) or you can add @Query to a method and provide the filtering logic. See http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query – Balaji Krishnan Mar 16 '16 at 12:04
  • @BalajiKrishnan for example I'll add a following method: `findByCardCategoryAndLevelAndCardDropRate(cardCategory, level, cardDropRate)` - is any way there to retrieve only all `CardCategoryLevel` based on `cardDropRate` and skip filtering by `cardCategory` and `level` ? Or in order to do it I need to provide 3 different methods to my `CardCategoryLevelRepository` for all of the parameter combinations ? – alexanoid Mar 16 '16 at 12:08
  • I might be wrong, but AFAIK you need three methods – Balaji Krishnan Mar 16 '16 at 12:15

1 Answers1

0

Implemented with QueryDSL and Entity Graphs. Right now I can apply filtering using QueryDSL predicates(also, please vote for this BUG: https://jira.spring.io/browse/DATAJPA-684):

@Repository
public interface CardCategoryLevelRepository extends JpaRepository<CardCategoryLevel, Long>, QueryDslPredicateExecutor<CardCategoryLevel>, CardCategoryLevelRepositoryCustom {

    @Override
    @EntityGraph(value = "graph.CardCategoryLevel", type = EntityGraphType.LOAD)
    List<CardCategoryLevel> findAll(Predicate predicate);

....
}

public interface CardCategoryLevelRepositoryCustom {

    Page<CardCategoryLevel> findAll(Predicate predicate, Pageable pageable);

}

public class CardCategoryLevelRepositoryImpl extends SimpleJpaRepository<CardCategoryLevel, Long> implements CardCategoryLevelRepositoryCustom {

    private final EntityManager entityManager;
    private final EntityPath<CardCategoryLevel> path;
    private final PathBuilder<CardCategoryLevel> builder;
    private final Querydsl querydsl;

    /**
     * Workaround, must be removed once fixed
     * https://jira.spring.io/browse/DATAJPA-684
     * http://stackoverflow.com/questions/36043665/querydsl-query-specified-join-fetching-but-the-owner-of-the-fetched-associati
     * 
     * @param entityManager
     */
    @Autowired
    public CardCategoryLevelRepositoryImpl(EntityManager entityManager) {
        super(CardCategoryLevel.class, entityManager);

        this.entityManager = entityManager;
        this.path = SimpleEntityPathResolver.INSTANCE.createPath(CardCategoryLevel.class);
        this.builder = new PathBuilder<>(path.getType(), path.getMetadata());
        this.querydsl = new Querydsl(entityManager, builder);
    }

    /**
     * Workaround, must be removed once fixed
     * https://jira.spring.io/browse/DATAJPA-684
     * http://stackoverflow.com/questions/36043665/querydsl-query-specified-join-fetching-but-the-owner-of-the-fetched-associati
     */
    @Override
    public Page<CardCategoryLevel> findAll(Predicate predicate, Pageable pageable) {
        JPAQuery countQuery = createQuery(predicate);
        JPAQuery query = (JPAQuery) querydsl.applyPagination(pageable, createQuery(predicate));

        query.setHint(EntityGraph.EntityGraphType.LOAD.getKey(), entityManager.getEntityGraph("graph.CardCategoryLevel"));

        Long total = countQuery.count();
        List<CardCategoryLevel> content = total > pageable.getOffset() ? query.list(path) : Collections.<CardCategoryLevel> emptyList();

        return new PageImpl<>(content, pageable, total);
    }

    private JPAQuery createQuery(Predicate predicate) {
        return querydsl.createQuery(path).where(predicate);
    }

}
alexanoid
  • 24,051
  • 54
  • 210
  • 410