如何在需要获取内部另一个 DTO 的集合时使用 Spring 数据 JPA 投影

How to use Spring data JPA projections when a Collection of another DTO inside needs to be fetched

我有一个 DTO,它包含另一个 DTO 的集合。我想使用 Spring 数据 JPA 投影来仅获取此特定 DTO 所需的数据。但是我不知道怎么办。

更新:添加了实体 classes。问题是关于 Spring 数据预测,而不是一般的 Spring 数据 jpa。

@Getter
@Setter
@NoArgsConstructor
public class RestaurantDto {
    private int id;
    private String name;
private String address;
private int votes;
private List<DishDtoShort> dishes;


public RestaurantDto(String name, String address) {
    this.name = name;
    this.address = address;
}

public RestaurantDto(int id, String name, String address,int votes, List<DishDtoShort> dishes) {
    this.votes = votes;
    this.id = id;
    this.name = name;
    this.address = address;
    this.dishes = dishes;
}

public void addDish(DishDtoShort dishDtoShort) {
    dishes.add(dishDtoShort);
}
public List getDishes() {
    return Collections.unmodifiableList(dishes);
}

}

集合中的 DTO

@Getter
@Setter
@NoArgsConstructor
public class DishDtoShort {
    private int id;
    private String name;
    private double price;

    public DishDtoShort(int id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }
}

餐厅实体

@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString(exclude = {"votes", "dishes"})
public class Restaurant extends AbstractNamedEntity {

    String address;

    @OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonManagedReference
    private List<Dish> dishes;

    @OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonManagedReference
    private List<Vote> votes;

    public void addDish(Dish dish) {
        this.dishes.add(dish);
        dish.setRestaurant(this);
    }

    public void removeDish(Dish dish) {
        this.dishes.remove(dish);
        dish.setRestaurant(null);
    }

    public void addVote(Vote vote) {
        this.votes.add(vote);
        vote.setRestaurant(this);
    }

    public void removeVote(Vote vote) {
        this.votes.remove(vote);
        vote.setRestaurant(null);
    }

    public Restaurant(String name, String address) {
        super(name);
        this.address = address;
    }



    public List<Dish> getDishes() {
        return Collections.unmodifiableList(dishes);
    }

    public List<Vote> getVotes() {
        return Collections.unmodifiableList(votes);
    }
}

盘子实体

@Entity
 @Getter
 @Setter
 @ToString
 @NoArgsConstructor
public class Dish extends AbstractNamedEntity {
    private double price;
    private LocalDate dateAdded;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonBackReference
    private Restaurant restaurant;

    public void setRestaurant (Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    public Dish(String name, double price, Restaurant restaurant) {
        super(name);
        this.price = price;
        this.dateAdded = LocalDate.now();
        this.restaurant = restaurant;
    }
}

P.S。我知道错误的 class 命名。

我认为,您只需在您的存储库中定义您的 DTO,并且不要忘记在您的实体和 DTO 上使用相同的名称进行映射:

@Repository
public interface RestaurantRepository extends JpaRepository<Restaurant, Integer> {
    List<RestaurantDto> findByName(String name);
}

您可以在这里获得更多信息:https://www.baeldung.com/spring-data-jpa-projections