Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 存储库中的Spring数据过滤方法_Java_Spring_Jpa_Spring Data_Spring Data Jpa - Fatal编程技术网

Java 存储库中的Spring数据过滤方法

Java 存储库中的Spring数据过滤方法,java,spring,jpa,spring-data,spring-data-jpa,Java,Spring,Jpa,Spring Data,Spring Data Jpa,在我的Spring Boot/Data/JPA 2.1应用程序中,我有以下实体: @Entity @NamedEntityGraph(name = "graph.CardCategoryLevel", attributeNodes = { @NamedAttributeNode("cardCategory"), @NamedAttributeNode("level") }) @Table(name = "card_categories_levels") public class CardCate

在我的Spring Boot/Data/JPA 2.1应用程序中,我有以下实体:

@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>();
....
}
基于
CardCategoryLevelRepository.findAll(Pageable Pageable)
我可以通过分页和排序检索所有
CardCategoryLevel


现在我不知道如何将过滤应用到这种方法中。例如,我需要能够通过
cardcontegory
或/和
Level
或/和
cardroprate
过滤
cardcontegorylevel
。如何将过滤功能注入
CardCategoryLevel Repository.findAll(Pageable Pageable)
方法,或者可能是一种新的类似方法?

使用QueryDSL和实体图实现。现在我可以使用QueryDSL谓词应用过滤(另外,请投票支持这个BUG:):

@存储库
公共接口CardCategoryLevelRepository扩展了JpaRepository、QueryDSL谓词执行器、CardCategoryLevelRepositoryCustom{
@凌驾
@EntityGraph(value=“graph.CardCategoryLevel”,type=EntityGraphType.LOAD)
列表findAll(谓词);
....
}
公共接口CardCategoryLevelRepositoryCustom{
pagefindall(谓词谓词,可分页);
}
公共类CardCategoryLevel RepositoryImpl扩展了SimpleParepository实现CardCategoryLevel RepositoryCustom{
私人最终实体管理人实体管理人;
私有最终实体路径;
私有最终路径生成器;
私人最终查询码查询码查询码查询码;
/**
*修复后,必须将其移除
* 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
*/
@自动连线
公共卡类别VelrepositoryImpl(EntityManager EntityManager){
超级(CardCategoryLevel.class、entityManager);
this.entityManager=entityManager;
this.path=SimpleEntityPathResolver.INSTANCE.createPath(CardCategoryLevel.class);
this.builder=新的PathBuilder(path.getType(),path.getMetadata());
this.querydsl=新的querydsl(entityManager,builder);
}
/**
*修复后,必须将其移除
* https://jira.spring.io/browse/DATAJPA-684
* http://stackoverflow.com/questions/36043665/querydsl-query-specified-join-fetching-but-the-owner-of-the-fetched-associati
*/
@凌驾
公共页findAll(谓词谓词,可分页){
JPAQuery countQuery=createQuery(谓词);
JPAQuery query=(JPAQuery)querydsl.applyPagination(可分页,createQuery(谓词));
setHint(EntityGraph.EntityGraphType.LOAD.getKey(),entityManager.getEntityGraph(“graph.CardCategoryLevel”);
Long total=countQuery.count();
List content=total>pageable.getOffset()?query.List(路径):Collections.emptyList();
返回新的PageImpl(内容、可分页、总计);
}
私有JPAQuery createQuery(谓词){
返回querydsl.createQuery(路径).where(谓词);
}
}

您可以创建一个方法名称,spring数据将根据该名称创建查询(将产生巨大的方法名称),也可以向方法添加@query并提供过滤逻辑。例如,请参见@BalajiKrishnan,我将添加以下方法:
findByCardCategoryAndLevelAndCardDropRate(cardCategory,level,cardDropRate)
-是否有任何方法可以基于
cardDropRate
仅检索所有
CardCategoryLevel
,并跳过
cardCategory
level
的过滤?或者为了做到这一点,我需要为我的
CardCategoryLevelRepository
所有参数组合提供3种不同的方法?我可能错了,但您需要三种方法
@Repository
public interface CardCategoryLevelRepository extends JpaRepository<CardCategoryLevel, Long> {

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

}
@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);
    }

}