Skip to content
Advertisement

JPA Entity that has a OneToMany relationship and want to use DTO

I have an entity called “Review” that has a OneToOne relationship with a “User” entity and a OneToMany relationship with a “ReviewStage” entity. I have implemented a DTO pattern so, I also have ReviewDTO which is actually what is being sent to the UI. I am using mapstruct to map the entity to dto. All is working well however, I would rather use the UserDTO and ReviewStageDTO in the relationship mappings.

This works well:

JavaScript

For fun, I tried this but, obviously doesn’t work:

JavaScript

I just need a nudge in the right direction. Thanks,

Advertisement

Answer

The relationships should be between entities only and if you want to make a dto for Review and inside this dto you want to return the UserDto for example you should create a mapstruct class to map between UserEntity to UserDTO

Example

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