I have 2 entities - movie and actor - that have M:N relationship. When designing DTO objects for these entities I am not sure what's the correct way to go.
Movie
@Entity
@Table(name = "Movies")
public class Movie extends AbstractBusinessObject {
private String name;
private String short_info;
@ManyToMany
private Map<String, Actor> cast;
}
Actor
@Entity
@Table(name = "Actors")
public class Actor extends Person{
private String name;
@ManyToMany(mappedBy = "cast")
private Set<Movie> movies;
}
Now concerning the DTOs: I've come across two different ways how to deal with 1:N and M:N relationships.
Saving only IDs:
public class MovieDto {
private String name;
private String short_info;
// Long represents Actor's ID
private Map<String, Long> cast;
}
However as said here I thought that Instead of performing many remote calls on EJBs, the idea was to encapsulate data in a value object
, and this approach clearly breaks the rule.
Saving DTOs in DTOs: Another approach would be to store Actor's Dto, instead of its ID.
public class MovieDto {
private String name;
private String short_info;
private Map<String, ActorDto> cast;
}
It seems to me that this approach would be faster, as I don't have to call the database everytime I need to show actor's name for example.
Is this assumption correct, or would it be better to store only IDs (even considering the space consumption of the second approach)?
Plus the second approach would result in a number of DTOs for one Entity. For instance, I don't need to know what movies has actor played in when looking at a "movie page", but I need to it when looking at an "actor page".