首页 > 解决方案 > 当需要获取内部另一个 DTO 的集合时如何使用 Spring 数据 JPA 投影

问题描述

我有一个 DTO,它有另一个 DTO 的集合。我想使用 Spring 数据 JPA 预测来仅获取此特定 DTO 所需的数据。但我不知道怎么做。

更新:添加了实体类。问题是关于 Spring Data Projections,而不是 Spring data 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;
    }
}

PS我知道错误的类命名。

标签: javaspringspring-bootspring-data-jpaprojection

解决方案


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

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

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


推荐阅读