Home > Software design >  Using multiple model objects in a java spring boot controller
Using multiple model objects in a java spring boot controller

Time:11-11

I'm currently trying to make a website, where I have Item models, and User models. I want to use the item controller to display information about a user and item, (which is stored in a mysql databse) on the same html page.

How would i go about this, without having to also inject the userService class into the itemService class?

CodePudding user response:

There is nothing stopping you from creating a service method that passes back a DTO containing both a User and an Item. You can inject multiple repositories into your service and retrieve both entities within the same transaction.

You could also provide separate methods in your service, one to get the user and the other to get the item, but in that case you would have two separate transactions.

CodePudding user response:

Thanks for the replies guys, it helped me out a lot. I was mainly just confused about the role of service classes I guess. Now I'm using multiple repositories for my service class, which then sends a DTO back to the controller, with all the data it needs.

  • Related