Create The Review Entity
Reviews are a key component of our restaurant platform, allowing users to share their experiences and ratings.
In this lesson, we'll create the Review entity that will store review content, ratings, and related data.
Understanding the Review Structure
The Review entity will contain several important pieces of information:
- The review content and rating
- Who wrote the review
- When it was posted and last edited
- Any photos attached to the review
Let's create our Review class with all these components.
Creating the Review Entity
Let's create our Review class in the com.devtiro.restaurant.domain.entities package:
package com.devtiro.restaurant.domain.entities;
import lombok.*;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Review {
@Field(type = FieldType.Keyword)
private String id;
@Field(type = FieldType.Text)
private String content;
@Field(type = FieldType.Integer)
private Integer rating;
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
private LocalDateTime datePosted;
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
private LocalDateTime lastEdited;
@Field(type = FieldType.Nested)
private List<Photo> photos = new ArrayList<>();
@Field(type = FieldType.Nested)
private User writtenBy;
}Understanding the Fields
The rating field uses FieldType.Integer, which is new to our application.
This field type is perfect for numeric values that we want to use in calculations, like computing average ratings.
For our date fields, we use the DateFormat.date_hour_minute_second format to capture precise timestamps of when reviews are posted and edited.
The photos field is initialized with an empty ArrayList to ensure we never have a null list, making it safer to work with.
Summary
- Created the
Reviewentity with content and rating fields - Added timestamp fields with proper
DateFormatconfiguration - Used
FieldType.Integerfor theratingfield - Included nested
PhotoandUserrelationships - Initialized the
photoslist for safe handling