【问题标题】:Get joined objects with Spring Data Jpa使用 Spring Data Jpa 获取连接对象
【发布时间】:2020-04-22 04:30:59
【问题描述】:

我正在编写一个基于 Spring-Boot 的 java 项目。 我有one-to-many 关系实体(我已经排除了getter 和setter):

@Entity
@Table(name = "restaurants")
public class Restaurant {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @NotBlank(message = "Please fill the name")
    private String name;

    @NotBlank(message = "Please fill the address")
    private String address;

    private LocalDateTime registered;

    private boolean isEnabled = true;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "restaurant")
    private List<Meal> meals;

    public Restaurant() {
    }

    public Restaurant(String name, String address, List<Meal> meals) {
        this.name = name;
        this.address = address;
        this.meals = meals;
    }
}

@Entity 
@Table(name = "meals") 
public class Meal {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String description;

    private Integer price;

    private LocalDate date;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "restaurant_id", nullable = false)
    private Restaurant restaurant;

    public Meal() {
    }

    public Meal(String description, Integer price, Restaurant restaurant) {
        this.description = description;
        this.price = price;
        this.restaurant = restaurant;
    } 
}

以下是创建表的 SQL 脚本:

CREATE TABLE restaurants
(
    id         INTEGER   DEFAULT nextval('hibernate_sequence') PRIMARY KEY,
    address    VARCHAR(255)            NOT NULL,
    name       VARCHAR(255)            NOT NULL,
    registered TIMESTAMP DEFAULT now() NOT NULL,
    is_enabled BOOLEAN   DEFAULT TRUE  NOT NULL
);

CREATE UNIQUE INDEX restaurants_unique_name_address_idx ON restaurants (name, address);

CREATE TABLE meals
(
    id            INTEGER DEFAULT nextval('hibernate_sequence') PRIMARY KEY,
    date          DATE    DEFAULT now() NOT NULL,
    description   VARCHAR(255)          NOT NULL,
    price         INTEGER               NOT NULL,
    restaurant_id INTEGER               NOT NULL,
    FOREIGN KEY (restaurant_id) REFERENCES restaurants (id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX meals_unique_restId_date_description_idx ON meals (restaurant_id, date, description);

我需要从存储库中获取所有餐厅:

public interface RestaurantRepository extends CrudRepository<Restaurant, Integer>

,但关联餐点必须只有今天的日期。

如何使用 Spring Data Jpa 完成?也许可以使用findBy()@Query 注释之类的方法 - 但我不知道它是如何工作的。

【问题讨论】:

    标签: java sql postgresql hibernate spring-data-jpa


    【解决方案1】:

    您的查询应该如下所示,

    public interface RestaurantRepository extends CrudRepository<Restaurant, Integer> {
    
        @Query("select distinct r from Restaurant r inner join r.meals m where m.date =:date")
        List<Restaurant> findByRestaurantMealDate(@Param("date") LocalDate date);
    
    }
    

    现在去取它,

    List<Restaurant> restaurants = repository.findByRestaurantMealDate(LocalDate.now());
    

    【讨论】:

    • 我稍微改写了你的代码,它可以工作了!非常感谢!
    【解决方案2】:
    @Query("select distinct r from Restaurant r join fetch r.meals m where m.date=?1")
    List<Restaurant> findByRestaurantMealDate(LocalDate date);
    

    有效!谢谢大家!

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 2019-11-18
      • 2013-03-07
      • 2015-06-03
      • 2018-11-17
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多