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:

  • @Data generates common methods like getters, setters, and toString
  • @AllArgsConstructor and @NoArgsConstructor provide constructors we'll need
  • @Builder enables the creation of User objects using the builder pattern

Elasticsearch Field Types

We use two different Elasticsearch field types in our entity:

  • FieldType.Keyword for the id field - this type is used for exact matches and aggregations
  • FieldType.Text for 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 @Field annotations for Elasticsearch
  • Used FieldType.Keyword for exact matching of IDs
  • Used FieldType.Text for searchable name fields
© 2026 Devtiro Ltd. All rights reserved