I am using java+Spring framework+Hibernate for creating rest api but I have stumbled upon retrieving details of a table using foreign key attributes. I have the following tables::
https://i.stack.imgur.com/lG7UR.png
I am retrieving all the ratings given using product id and then mapping to DTO, now I also want to populate the username using idusers as this is my foreign key.
Same is the case when I try to retrieve ratings given by the users, instead of displaying idproducts I want to display the product name and product description as It is a foreign key.
Any advice on how to do so using DTO's.
You can use ModelMapper when converting a DTO to an Entity bean and back from Entity bean to a DTO.
Add ModelMapper to your project
Define the ModelMapper bean in your Spring configuration
Assuming the following models based on the given ER diagram you have given
You can retrieve the ratings of a product using product id along with the user details by using the following method
Then mapping rating objects to DTO
Now you can retrieve username as following
The same way you can retrieve the ratings by userId and access product details