Adding Properties to User
Learn how to add more properties to an Entity.
We'll cover the following...
We'll cover the following...
Adding properties to User
So far, our User
only has a surrogate primary key (the id
). Let’s add some more fields like gender, birthday, email and phone number to make things more interesting:
Press + to interact
package com.tamingthymeleaf.application.user;import io.github.wimdeblauwe.jpearl.AbstractEntity;import javax.persistence.Entity;import javax.persistence.EnumType;import javax.persistence.Enumerated;import javax.persistence.Table;import javax.validation.constraints.NotNull;import java.time.LocalDate;@Entity@Table(name = "tt_user")public class User extends AbstractEntity<UserId> {@NotNullprivate UserName userName; //<.>@NotNull@Enumerated(EnumType.STRING)private Gender gender; //<.>@NotNullprivate LocalDate birthday; //<.>@NotNullprivate Email email; //<.>@NotNullprivate PhoneNumber phoneNumber; //<.>protected User() {}public User(UserId id,UserName userName,Gender gender,LocalDate birthday,Email email,PhoneNumber phoneNumber) {super(id);this.userName = userName;this.gender = gender;this.birthday = birthday;this.email = email;this.phoneNumber = phoneNumber;}public UserName getUserName() {return userName;}public Gender getGender() {return gender;}public LocalDate getBirthday() {return birthday;}public Email getEmail() {return email;}public PhoneNumber getPhoneNumber() {return phoneNumber;}}
UserName
is a value object that contains thefirstName
andlastName
of a user.Gender
is an enum for the possible genders