What should I put in UserDTO and why?

Advertisements

I have a User entity as following:

package com.yogesh.juvenilebackend.Model;

import jakarta.annotation.Generated;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public class User {

    @Id
    @GeneratedValue
    @Column(updatable = false, nullable = false)
    private String id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false, unique = true)
    private String email;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false, unique = true)
    private String userName;

}

What fields should I create inside the userDTO and why? What unique constraints I should add into the DTO?

I have currently made this DTO:

package com.yogesh.juvenilebackend.DTO;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;


@AllArgsConstructor
@Setter
@Getter
public class UserDTO {
    @NonNull
    private String userName;
    @NonNull
    private String name;
    @NonNull
    private String email;
}

What should I change in it? Also, what annotations I should add in UserDTO and User member fields and what constructors I should add?

>Solution :

DTO, which stands for Data Transfer Object, is a container for data. It has no logics and only has getter and setter functions. For example, let’s say getUser() returns information about the user. Then getUser() return DTO containing information of username, name, and email and you can access them with getter and setter.

Leave a ReplyCancel reply