Skip to content
Advertisement

Spring jpa repository returns the entity class instead of the dto interface

I am using Spring data jpa in the backend codes. I have included entities, dto interfaces, service and jpa repository codes.

Now the issue is that when I call getAllTopics() in the TopicService. It returns a list of Topic object instead of TopicDto. Topic object includes a list of examples which I do not include in the TopicDto. And Topic object also includes a list of Comment object instead of CommentDto.

This only happens when I add Set<CommentDto> getComments() in TopicDto. If I remove it, everything work fine. Can anyone tell me how should I map the dto in my service and repository class? Why does it return entity class instead of dto?

JavaScript
JavaScript
JavaScript
JavaScript
JavaScript
JavaScript

Advertisement

Answer

First thing is to change your TopicRepository to use Topic, the actual entity, and not TopicDto:

JavaScript

Then, you need the following DTO classes:

JavaScript
JavaScript

Finally, in your TopicService you need to do the mapping from Topic to TopicDto as follows:

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