簡體   English   中英

Spring Data JPA項目,如何限制嵌套對象的數量? @一對多

[英]Spring Data JPA project, how to limit the amount of nested objects? @OneToMany

我正在開發一個小型 Spring REST API 項目。 我有兩個類代表我的數據庫中的 2 個表。 我在要從中檢索數據的對象中有一個 @OneToMany 映射。 現在我檢索所有嵌套對象,但我想要的是能夠通過它的 int datestamp 變量(這是一個在我的類中聲明為“utcs”的紀元)來限制嵌套對象的數量。 我天真地認為 CrudRepoitory 可以幫助我解決這個問題,但現在我明白我錯了。 我希望能夠在我的存儲庫中做的事情是這樣的:

@Repository
public interface TypeRepository extends CrudRepository<Type, Integer> {
    List<Type> findByDataUtcsGreaterThan(int utcs);
}

這是我想要的 JSON 結構以及它現在的樣子。 但是如何限制 Data 對象的數量?

[
    {
        "typeKey": "Queue",
        "uomId": 1,
        "data": [
            {
                "value": 11,
                "utcs": 1605840300
            },
            {
                "value": 15,
                "utcs": 1605840360
            },
            {
                "value": 22,
                "utcs": 1605840420
            }
        ]
    },
    {
        "typeKey": "Unroutable",
        "uomId": 1,
        "data": [
            {
                "value": 196,
                "utcs": 1605840300
            },
            {
                "value": 196,
                "utcs": 1605840360
            },
            {
                "value": 196,
                "utcs": 1605840420
            }
        ]
    }
]

帶有嵌套對象 @OneToMany 的 (Type) 對象類

@Entity
@Table(name = "SYSTEMSTATSTYPE")
public class Type {
    @Id
    @Column(name = "ID")
    private int id;
    @Column(name = "TYPEKEY")
    private String typeKey;
    @Column(name = "UOMID")
    private int uomId;

    @OneToMany(mappedBy = "type", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Data> data = new ArrayList<>();

    public Type() {
        super();
    }

    public Type(String typeKey, int uomId) {
        this.typeKey = typeKey;
        this.uomId = uomId;
    }

    // Getters and setters
}

(Data) 對象類@ManyToOne

@Entity
@Table(name = "SYSTEMSTATSDATA")
public class Data {
    @Id
    @Column(name = "ID")
    private int id;
    @Column(name = "VALUE")
    private int value;
    @Column(name = "UTCS")
    private int utcs;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "TYPEID")
    private Type type;


    public Data() {
        super();
    }

    public Data(int value, int utcs, Type type) {
        super();
        this.value = value;
        this.utcs = utcs;
        this.type = type;
    }
    // Getters and setters
}

我不認為直接使用注釋是可能的,並且在大多數情況下使用 EAGER fetching 無論如何都不是一個好主意。 我認為您必須使用自定義查詢自己構建邏輯,或者只獲取 2 個數據對象(如此處描述的內容https://www.baeldung.com/jpa-limit-query-results ),或者加入讓您立即將 Type 和 Data 放在一起。

這不是直接可能的。 你可以為每個Type對象做專門的選擇查詢,或者使用像Blaze-Persistence Entity Views這樣的東西,它對限制集合元素有本機支持。

我創建了該庫以允許在 JPA 模型和自定義接口或抽象類定義的模型之間輕松映射,例如類固醇上的 Spring Data Projections。 這個想法是您按照自己喜歡的方式定義目標結構(域模型),並通過 JPQL 表達式將屬性(getter)映射到實體模型。

使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:

@EntityView(Type.class)
public interface TypeDto {
    @IdMapping
    Integer getId();
    String getTypeKey();
    int getUomId();
    @Limit(limit = "5", order = "utcs DESC")
    Set<DataDto> getData();

    @EntityView(Data.class)
    interface DataDto {
        @IdMapping
        Integer getId();
        int getValue();
        int getUtcs();
    }
}

查詢是將實體視圖應用於查詢的問題,最簡單的只是按 id 查詢。

TypeDto a = entityViewManager.find(entityManager, TypeDto.class, id);

Spring Data 集成允許您像使用 Spring Data Projections 一樣使用它: https : //persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

@Repository
public interface TypeRepository extends CrudRepository<Type, Integer> {
    List<TypeDto> findByDataUtcsGreaterThan(int utcs);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM