Implementing the domain model User using JPA annotations will look like the following:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@Id
@GeneratedValue
private Integer id;
@Column(unique = true)
private String username;
@Column(unique = true)
private String email;
private String password;
}
In the preceding code, the @Entity annotation is used to mark the User class as a JPA entity so that it will be eligible to be used in the JPA persistence environment. The @Data annotation is from the Lombok library and is used to mark a POJO as a class that will hold data. This means getters, setters, the equals method, the hashCode method, and the toString method will be generated for that class.
The @Id annotation marks the ID property as the identity field of the entity, whereas @GeneratedValue marks it as an auto-generated value. The newly added annotations, which are also from the Lombok library, are @AllArgsConstructor, which will generate a constructor with the id, username, password, properties, and @NoArgsConstructor, which will generate a default constructor.