Create The User Entity
In this lesson, we'll create a User entity that will store basic user information within our Restaurant Review Platform.
Understanding the User Entity Design
While Keycloak will handle user authentication and management, we need a lightweight User entity to store basic user information within our application.
This approach allows us to efficiently access user details when displaying restaurants and reviews, without making additional calls to Keycloak.
However, it's important to note that this creates eventually consistent data - if a user updates their details in Keycloak, our stored information won't automatically update.
Creating the User Entity
Let's start by creating a new package to store our entity classes:
package com.devtiro.restaurant.domain.entities;Now, let's create our User class with Lombok annotations to reduce boilerplate code:
@Data // Generates getters, setters, toString, equals, and hashCode
@AllArgsConstructor // Generates a constructor with all arguments
@NoArgsConstructor // Generates a no-args constructor
@Builder // Enables the builder pattern
public class User {
@Field(type = FieldType.Keyword)
private String id;
@Field(type = FieldType.Text)
private String username;
@Field(type = FieldType.Text)
private String givenName;
@Field(type = FieldType.Text)
private String familyName;
}Understanding the Annotations
The Lombok annotations we're using serve specific purposes:
@Datagenerates common methods like getters, setters, andtoString@AllArgsConstructorand@NoArgsConstructorprovide constructors we'll need@Builderenables the creation of User objects using the builder pattern
Elasticsearch Field Types
We use two different Elasticsearch field types in our entity:
FieldType.Keywordfor theidfield - this type is used for exact matches and aggregationsFieldType.Textfor name fields - this type is better for full-text search and partial matches
Summary
- Created a User entity to store basic user information
- Used Lombok annotations to reduce boilerplate code
- Configured
@Fieldannotations for Elasticsearch - Used
FieldType.Keywordfor exact matching of IDs - Used
FieldType.Textfor searchable name fields