Discussion on Entity and DTO

1) is it better for repository to return entity or dto?

2) is the design in entity as good as the table structure in the database? Is it better to use associated objects?

example: define Entity entities
method 1:

@Entity
class Duty {

   private Long creator;  // ID
   private Integer state;  // ID
   ...
}

method 2:

@Entity
class Duty {
    private Staff creator;  // 
    private State state;  // 
}

do you think way 1 or way 2?
personally think:

  1. Mode 1 is more convenient when saving, while Mode 2 needs to create a corresponding Staff
when saving.

, State object

  1. mode 2 can be returned directly to the front end when returning, while mode 1 still needs data processing when returning. After all, only ID value is taken out, and text value is also needed (how to deal with this? each record gets the text value from the database according to ID? )

what do you do when designing objects that store id and text separately? Are there any good design codes?

Jun.15,2022

repository returns DO
service returns DTO
excluding hibernate, repository is closer to the data layer and does not consider the hyperemia model, so it should be more concise. On the one hand, it corresponds directly to the data, on the other hand, it will not cause interference because of interdependence and inability to assign values in microservices.
service constructs DTO for passing

Menu