Bonjour,
Je fais face a un cas plutot tordu pour convertir mes POJO JPA en DTO.
Mon modele est le suivant: un Project contient 0..n ProjectDetail.
Mes POJO annoté sont convertit en DTO pour être affiché plus tard.
Voici le code de chacun (avec juste les parties interessantes):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public class Project implements Serializable {
private static final long serialVersionUID = 3278578205097992228L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@OneToMany(mappedBy="project")
private List<ProjectDetails> projectDetails;
/**
* Convertit un objet modele JPA en objet Service DTO.
* @return un objet DTO
*/
public ProjectDTO modelToDTO() {
List<ProjectDetailsDTO> projectDetailsDTO = new ArrayList(projectDetails.size());
for (ProjectDetails projectDetailsTmp : projectDetails) {
projectDetailsDTO.add(projectDetailsTmp.modelToDTO());
}
ProjectDTO dto = new ProjectDTO(this.id, this.client, this.featured, projectDetailsDTO);
return dto;
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class ProjectDetails implements Serializable {
private static final long serialVersionUID = 7272919421369573151L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@ManyToOne
@JoinColumn(name = "PROJECT_ID", nullable=false)
private Project project;
/**
* Convertit un objet modele JPA en objet Service DTO.
* @return un objet DTO
*/
public ProjectDetailsDTO modelToDTO() {
ProjectDetailsDTO dto = new ProjectDetailsDTO(id, project.modelToDTO(), locale, title, description, image, url, testimony);
return dto;
} |
Le probleme est que pour transformer un Project en ProjectDetails je dois transformer la liste de ProjectDetails de Project en liste de ProjectDetailsDTO.
Donc pour chaque element de la liste j'appelle la méthode modelToDTO de ProjectDetails. Mais pour créer un nouvel objet ProjectDTO dans le ProjectDetailsDTO je fais un appel a project.modelToDTO d'ou la boucle infinie...
Je ne sais pas si je suis clair mais je pense que le probleme est au niveau conception/modélisation. Avez vous des idées ou une solution ?
Merci
Partager