#What should I include in DTO?

1 messages · Page 1 of 1 (latest)

dire maple
#

I am currently working on a hobby project called Online School Management System where I am performing basic CRUD operations. I have limited knowledge of relationship mapping in Spring boot. There are 4 entities, student, teacher, course and classroom. I have mapped the relationship between entities accordingly. Should I include the relationship in my DTOs as well. For example: This is my course entity

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Course implements Updatable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long courseId;
    private String name;
    private Integer capacity;
    private LocalDate startDate;
    private LocalTime startTime;

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private Teacher teacher;

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE},fetch = FetchType.LAZY)
    private Set<Student> students = new HashSet<>();

    @ManyToOne(cascade = CascadeType.ALL)
    private Classroom classroom;
}

and This is my course DTO

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CourseDto implements Updatable {
    private Long courseId;
    private String name;
    private Integer capacity;
    private LocalDate startDate;
    private LocalTime startTime;

    private TeacherDto teacher;
    private Set<StudentDto> students;

}```
acoustic zodiacBOT
# dire maple I am currently working on a hobby project called Online School Management System...

Detected code, here are some useful tools:

Formatted code
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Course implements Updatable {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY) private Long courseId;
  private String name;
  private Integer capacity;
  private LocalDate startDate;
  private LocalTime startTime;
  @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Teacher teacher;
  @ManyToMany(cascade = {
    CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE}
  , fetch = FetchType.LAZY) private Set<Student> students = new HashSet<>();
  @ManyToOne(cascade = CascadeType.ALL) private Classroom classroom;
}
#

<@&1004656351647117403> please have a look, thanks.

plucky orchid
#

drop the 2 lombok constructor annotations on the dto and u do not need any mapping on it. And add your missing field @dire maple 😉

formal prawn
#

Maybe a slight bias, but I also prefer immutable DTOs (records if possible)

sudden echo
dire maple
#

I tried putting everything in DTO. But when I tried deleting a student, there was some null constraint violation. And when I tried to get a studentby Id, there was stack overflow error because the mapper function implementation of different entities and DTOs were calling each other causing infinite recursion.

#

There were similar cases for all the other entities

sudden echo
#

Well yes, don't put circular references in a dro

#

And for the null constraints, it means you made a mistake somewhere, you allowed a null value at a place which shouldn't have one

dire maple
wary meteor
wary meteor