Search code examples
design-patternsdto

Can I have entity as one of the value in DTO


I need to pass few data between layers of my application and within layer.I have defined DTO to do this. My question is can I have my domain entity is one of Value in my DTO.Is it good design?

In below code UserEntity is JPA entity.

UserDTO.java

private UserEntity userEntity;

private String empNo;

private String place;


Solution

  • Ideally, no.

    DTO, Data Transfer Object, should ideally be decoupled from Database entities. That way, if you change anything in the latter, that information doesn't automatically flow into the former. This helps in preventing unnecessary data leaks.