Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
在(Collection<;?>;)方法中使用JPA规范时未返回结果_Jpa_Spring Data Jpa - Fatal编程技术网

在(Collection<;?>;)方法中使用JPA规范时未返回结果

在(Collection<;?>;)方法中使用JPA规范时未返回结果,jpa,spring-data-jpa,Jpa,Spring Data Jpa,我正在尝试使用Spring数据JPA规范实现此SQL查询的等效功能: SELECT * FROM product WHERE category_id IN (....) 此查询中涉及的两个实体具有OneToMany和manytone关系: 产品实体: @Data @NoArgsConstructor @Entity @Table(name = "PRODUCT") public class ProductEntity extends AbstractAuditableEnt

我正在尝试使用Spring数据JPA规范实现此SQL查询的等效功能:

SELECT * FROM product WHERE category_id IN (....)
此查询中涉及的两个实体具有
OneToMany
manytone
关系:

产品实体:

@Data
@NoArgsConstructor
@Entity
@Table(name = "PRODUCT")
public class ProductEntity extends AbstractAuditableEntity {

    // skipped other properties for simplicity sake

    @ManyToOne
    private CategoryEntity categoryEntity;

}
类别:

@Entity
@Table(name = "PRODUCT_CATEGORY")
@Data
public class CategoryEntity extends AbstractBaseEntity {

    // skipped other properties for simplicity sake

    @OneToMany(mappedBy = "categoryEntity")
    private List<ProductEntity> productEntities;

}
@实体
@表(name=“产品类别”)
@资料
公共类CategoryEntity扩展了AbstractBaseEntity{
//为了简单起见,跳过了其他属性
@OneToMany(mappedBy=“类别实体”)
私营实体;
}
My JPA Specifications query编译并运行时没有任何错误,但不会返回任何结果: 规范定义:

public static Specification<ProductEntity> inCategories(List<Long> categories) {
    return (root, query, builder) -> {
        if (categories != null && !categories.isEmpty()) {
            final Path<CategoryEntity> category = root.get("categoryEntity");
            return category.get("id").in(categories);
        } else {
            // always-true predicate, means that no filtering would be applied
            return builder.and();
        }
    };
}
类别(列表类别)中的公共静态规范{
返回(根、查询、生成器)->{
if(categories!=null&&!categories.isEmpty()){
最终路径类别=root.get(“categoryEntity”);
退货类别。在(类别)中获取(“id”);
}否则{
//始终为true谓词,表示不应用任何筛选
返回builder.and();
}
};
}
客户端代码:

    Page<ProductEntity> productEntityPage = productRepository.findAll(Specification
                    .where(ProductSpecifications.inCategories(filterCriteria.getCategories()))
            , pageRequest);
Page productEntityPage=productRepository.findAll(规范
.where(ProductSpecifications.inccategories(filterCriteria.getCategories()))
,第页(请求);
为什么不起作用?我在使用SQL语句查询数据库时会得到结果,因此我的JPA规范查询或实体映射一定有问题


我错过了什么?

我想你应该用join来这里

Join<ProductEntity, CategoryEntity> categoryJoin = root.join("categoryEntity");
return categoryJoin.get("id").in(categories);
Join categoryJoin=root.Join(“categoryEntity”);
返回(类别)中的categoryJoin.get(“id”);
而不是

Path<CategoryEntity> category = root.get("categoryEntity");
return category.get("id").in(categories);
Path category=root.get(“categoryEntity”);
退货类别。在(类别)中获取(“id”);

因为
root.get(“categoryEntity”).get(“id”)
将不提供任何信息,因为
product
表中不存在这样的路径(
product.categoryEntity.id
)。

尝试打开SQL调试并在日志中搜索JPA查询转换为生成的查询的确切SQL查询。旁注:您有(及其相反)这可能比
builder.and()。