如何在JPA存储库中使用规范编写自定义的findAll()

时间:2020-04-26 14:30:31

标签: spring jpa criteria

当我使用skuRepository.getAll()时,它可以正常工作,但是当我应用规范中定义的过滤器时(列表filteredRegs = skuRepository.getAll(specification)),我仍然获得表的所有行 我应该怎么做才能将规范应用于我的自定义方法?

public interface SkuRepository extends CrudRepository<Sku, Integer>, JpaSpecificationExecutor<Sku> {

    @Query("select s from Sku s join fetch s.unit un join fetch s.supplier sup WHERE un.id = sku_unit_id AND sup.id = supplier_id")
    List<Sku> getAll(@Nullable Specification<Sku> var1);

    @Query("select s from Sku s join fetch s.unit un join fetch s.supplier sup WHERE un.id = sku_unit_id AND sup.id = supplier_id")
    List<Sku> getAll();
}

UPD: 这是我的实体。 当我使用规范API通过Sku表进行抽样时,在日志中看到三个单独的选择:一个用于Sku实体,一个用于单位,一个用于供应商。我希望我的应用对联接进行选择。 我读到这是由于我使用EAGER访存类型,所以我将其更改为LAZY,但随后又遇到另一个问题:“ InvalidDefinitionException:未找到序列化程序...”这是合乎逻辑的,因为相关的实体Unit和Supplier是未加载。 然后,我决定编写带有请求的自定义getAll():

@Query("select s from Sku s join fetch s.unit un join fetch s.supplier sup WHERE un.id = sku_unit_id AND sup.id = supplier_id ORDER BY s.name")

但是现在它不支持规范。 请告知该怎么办。

@Entity
@Table(name = "sku")
public class Sku implements Cloneable, CloneableEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "sku_code", length = 6, nullable = false, unique = true)
    private String code;

    @Column(name = "sku_name", nullable = false)
    private String name;

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinColumn(name = "sku_unit_id", nullable = false)
    private Unit unit;

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", nullable = false)
    private Supplier supplier;

    @Column(name = "qty_in_sec_pkg")
    private int quantityInSecondaryPackaging;

    @Column(name = "sku_is_active", nullable = false)
    private boolean isActive;

//constructors, getters, setters

}

@Entity
@Table(name = "units")
public class Unit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id ")
    private int id;

    @Column(name = "unit", nullable = false, unique = true)
    private String unit;

    @Column(name = "description")
    private String description;

//constructors, getters, setters
}

@Entity
@Table(name = "suppliers")
public class Supplier {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id ")
    private int id;

    @Column(name = "supplier_code", length = 6, nullable = false, unique = true)
    private String supplierCode;

    @Column(name = "supplier_name", nullable = false)
    private String name;

    @Column(name = "create_date", length = 19, nullable = false)
    private String createDate;

    @Column(name = "update_date", length = 19)
    private String updateDate;
//constructors, getters, setters
}

1 个答案:

答案 0 :(得分:0)

您不能混用@Query和Specification

您只能使用JpaSpecificationExecutor接口方法来使用规范。

查找更多详细信息here

相关问题