DTO

Can anyone help me to create DTO for this classes and also explain me why we use DTO more
package com.ShelfSpace.ShelfSpace.model;

import java.util.List;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import jakarta.persistence.JoinColumn;

@Entity
@Table(name = "registered_user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {


@Column(name = "name", nullable = false)
private String name;

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


@Column(name = "password", nullable = false)
private String password;

@ManyToMany
@JoinTable(
name = "user_info_user_roles",
joinColumns = @JoinColumn(name = "user_info_email", referencedColumnName = "email"),
inverseJoinColumns = @JoinColumn(name = "user_roles_id")
)
private List<UserRoleEntity> roles;

}
package com.ShelfSpace.ShelfSpace.model;

import java.util.List;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import jakarta.persistence.JoinColumn;

@Entity
@Table(name = "registered_user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {


@Column(name = "name", nullable = false)
private String name;

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


@Column(name = "password", nullable = false)
private String password;

@ManyToMany
@JoinTable(
name = "user_info_user_roles",
joinColumns = @JoinColumn(name = "user_info_email", referencedColumnName = "email"),
inverseJoinColumns = @JoinColumn(name = "user_roles_id")
)
private List<UserRoleEntity> roles;

}
package com.ShelfSpace.ShelfSpace.model;

import java.util.List;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "user_roles")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserRoleEntity {

@Id
@Column(name = "role_id")
private int roleId;

@Column(name = "role_name", nullable = false, unique = true)
private String roleName;

@ManyToMany(mappedBy = "roles")
private List<UserEntity> registeredUser;
}
package com.ShelfSpace.ShelfSpace.model;

import java.util.List;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "user_roles")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserRoleEntity {

@Id
@Column(name = "role_id")
private int roleId;

@Column(name = "role_name", nullable = false, unique = true)
private String roleName;

@ManyToMany(mappedBy = "roles")
private List<UserEntity> registeredUser;
}
8 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @Danix! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st5mo ago
Assuming you are using a recent Java and Spring version, you can just use records as DTOs:
public record UserDTO(Stromg name, String email, List<UserRoleDTO> roles){}
public record UserRoleDTO(int roleId, String roleName){}
public record UserDTO(Stromg name, String email, List<UserRoleDTO> roles){}
public record UserRoleDTO(int roleId, String roleName){}
Like these would be the DTO classes
Danix
DanixOP5mo ago
These are methods right so what to do with them ?
dan1st
dan1st5mo ago
classes If you want to (de)serialize your dara from/to JSON, you would use DTOs that allows you to have a JSON data format that deviates from your internal storage
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Danix
DanixOP5mo ago
Ok so I have to use DTO when I have to convert my entities to json format right but if I have to register the user from a form then should I have to use a DTO or make it simple ?
dan1st
dan1st5mo ago
It depends. DTOs can even be useful with forms
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server