2
I have two REST services (separate projects) responding to the following entities with JPA (@Entity) mapping and each with its own database.
Product(ID and TITLE) and Category (ID and TITLE)
I found some comments saying that the best thing would be to replicate the entities in each microservice. I believe this is not a good solution for code duplicity.
In this way, how to make the JPA relationship between the two entities (@Manytomany) each being in a different project? What would this architecture look like? What options?
How the JPA relationship is made between resident entities in different microservices? Is not done. In this question the concept of microservices has certainly not yet been captured.
– Caffé
@Caffé I understood that for example, in product I could have an object that stores the category, and in category a list of products objects but without performing the mapping Onetomany and Manytoone respectively. In this way, the integrity should be guaranteed and if you needed access to another service, to search for example the products by category access via REST. That’s right?
– gustavomr
I looked here at Sopt for an answer about Microservices that might be useful for you but I did not find anything very decent... Even the tag is not filled out yet. There are some answers where a part is cool but there’s a paragraph or two that screws with important parts of the concept. For now I can tell you: forget relationship between entities at the object level when talking about microservices. Each microservice responds to a business capability (a big requirement, so to speak) and does not depend on objects from other microservices to be able to do its job.
– Caffé
Yeah, I think I get it. Each microservice has its defined behavior, and how much you need you can access via REST another service to complete your answer.
– gustavomr
That, and only to complete. If a microservice has no utility without consulting another microservice, it is not a de facto microservice because it is not independent and does not serve itself by a business ability.
– Caffé
Contributing a little with the good answer of @Caffé. In fact one should not think about the relationship itself between entities when talking about different Microservices. It is possible that in a re-evaluation of the scenario it is understood which product and category should be under the responsibility of a single microservice, or even that for one of the services consulting this only interest to know the category as a mere description for example within the product itself. This discussion is very interesting and many suggest that DDD should be used to help separate contexts from services.
– hebertrfreitas