Skip to content
Advertisement

Mapping multiple DTO to entities – nested exception

I’m trying to map DTOs to entities. I created a service that only takes care of mapping objects – ObjectMapper. DTO objects have relationships with each other. When I map a single object, for example when I create User, Group, Note, everything works. But when I want to use a method that returns a Note with a specific ID – /notes/{id}, I get the following error.

Handler dispatch failed; nested exception is java.langStackOverflowError] with root cause

To get specific Note, I need to use this mapping method that also cause this error. As u can see, I have to also convert Group and Tags.

JavaScript

When I don’t have relationships defined as another DTO in the DTO class, but as an entity, everything works, since I don’t have to convert the DTO to an entity.

Do you know where I’m making a mistake when mapping? Am I making a mistake in mapping multiple objects at once?

ObjectMapper

JavaScript

Advertisement

Answer

You get StackOverFlowError because you end up with infinite recursive methods call and your application creates infinite amount of objects, so you just run out of memory:

1) your NoteEntityToDtoGet method gets Note‘s group and calls GroupEntityToDtoGet method on the Group object;
2) in GroupEntityToDtoGet method you get all Group‘s notes and call NoteConvertList method on them, which calls NoteEntityToDtoGet on each of the ‘Note’
3) step 1 again…
… the same cycle goes over and over without a stop until your stack memory, you know, overflows 🙂

So you should decide do your DTO classes really need to hold references to other entity collections.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement