ModelMapper Records -> Entity Class mapped null

460 views
Skip to first unread message

Fox Tech

unread,
Oct 29, 2023, 11:39:17 AM10/29/23
to modelmapper
Hello,
I just find out about ModelMapper and I am struggling to use it because It seems that it does not work Records -> Entity class. It only works DTO -> Entity class. Please see the example 

public static void test() {
ModelMapper modelMapper = new ModelMapper();
ReviewDTO reviewDTO = new ReviewDTO("222", "111", 2);
Review map = modelMapper.map(reviewDTO, Review.class);
System.out.println(map);
}
===========================================================
public record ReviewDTO(String title, String description, int starRating) {
}

============================================================
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Review {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String title;
private String description;
private int starRating;

@Override
public String toString() {
return "Review{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", starRating=" + starRating +
'}';
}
}
=============================================================

OUTPUT= Review{id=null, title='null', description='null', starRating=0}

\
Does anyone knows why? Is it just me or it does not work? I have searched on google and nothing.

Many thanks,
Fox.
\

Fox Tech

unread,
Oct 29, 2023, 11:55:57 AM10/29/23
to modelmapper
https://github.com/modelmapper/modelmapper/issues/99  

I found a similar problem... it does not on final such as Records

Guilherme Mendes Ramalho

unread,
Oct 29, 2023, 1:27:49 PM10/29/23
to model...@googlegroups.com
hello

My problem was solved as follows, in the modelmapper it is necessary to traverse the object. In this case I did it this way.

public ConsultaDTO createConsulta(@RequestBody ConsultaDTO consultaDTO)
{
Query queryRequest = mapper.map(queryDTO, Query.class);
//clinic
consultaRequest.getConsultorio().forEach(c ->c.setNumero(c.getNumero()));
consultaRequest.getConsultorio().forEach(c ->c.setData_hora(c.getData_hora()));
//doctor
consultaRequest.getMedico().forEach(c ->c.setCrm(c.getCrm()));
consultaRequest.getMedico().forEach(c ->c.setEspecialidade(c.getEspecialidade()));
consultaRequest.getMedico().forEach(c ->c.setIdade(c.getIdade()));
consultaRequest.getMedico().forEach(c ->c.setNome(c.getNome()));

//Write to memory
Query query = consultaService.createConsulta(queryRequest);
// convert entity to DTO
return mapper.map(query, QueryDTO.class);
}


my project github is this one.


https://github.com/guismeiram/Clinica_OneToMany

Thank you for the help!


Não contém vírus.www.avast.com

--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modelmapper/ef508ccb-6208-4e39-8e84-592b01499e23n%40googlegroups.com.
img_4.PNG
img_2.PNG
img_5.PNG
img_3.PNG
img_1.PNG
img_6.PNG
img_7.PNG

Guilherme Mendes Ramalho

unread,
Oct 29, 2023, 1:32:32 PM10/29/23
to model...@googlegroups.com
There is also a problem if you do not use this method. It is as follows, if you have this method the object is not inserted into the database, due to an id conflict.

public Long getAllId(){
return (id.equals(0) ? null:id);
}

Não contém vírus.www.avast.com

On Sun, Oct 29, 2023 at 12:55 PM Fox Tech <foxte...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages