Java – Use Spring Data Jpa to get the connection object

Use Spring Data Jpa to get the connection object… here is a solution to the problem.

Use Spring Data Jpa to get the connection object

I’m writing a Spring-Boot-based Java project.
I have one-to-many relationship entities (I exclude getters and setters):

@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;
    } 
}

The following is the SQL script to create the table:

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);

I need to get all restaurants from the repository:

public interface RestaurantRepository extends CrudRepository<Restaurant, Integer>

, but the associated meal must only have today’s date.

How to do it with Spring Data Jpa? Maybe you could use something like findBy() or @Query comment – but I don’t know how that works.

Solution

Your query should look like this

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);

}

Get it now

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

Related Problems and Solutions