Create Restaurant Repository
Now that we have our Restaurant entity and all its related entities set up, we need a way to store and retrieve restaurant data from Elasticsearch.
We'll create a repository interface that gives us the basic operations we need to work with restaurant data.
Implementing the Restaurant Repository
The repository interface will serve as our gateway to perform operations on restaurant data in Elasticsearch. Here's how we'll implement it:
package com.restaurant.review.repository;
import com.restaurant.review.entity.Restaurant;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RestaurantRepository extends ElasticsearchRepository<Restaurant, String> {
// Custom queries will be added in future lessons
}The @Repository annotation marks this interface as a Spring Data repository bean.
This tells Spring to create an implementation of this interface at runtime.
Even though our repository looks simple now, it already provides many useful operations inherited from ElasticsearchRepository:
save()- Saves a restaurant or updates it if it already existsfindById()- Finds a restaurant by its IDfindAll()- Retrieves all restaurantsdelete()- Removes a restaurantcount()- Returns the total number of restaurants
In future lessons, we'll expand this repository by adding custom queries for searching restaurants by name, location, cuisine type, and other criteria.
Summary
- Created the
RestaurantRepositoryinterface extendingElasticsearchRepository - Added
@Repositoryannotation for Spring configuration - Gained access to basic CRUD operations through inheritance
- Set up foundation for future custom query methods