I would like to build a microservice architecture. I am using Java technologies, such as Spring, Hibernate, etc. In this scenario, I have entities as Java classes. For instance, A microservice has A entity, B microservice has B entity, and so on. So, if there is a one-to-many relationship between A and B, I need to import one of the classes into other class to declare it.
// in A microservice
// need to import class of B like "import com.project.B", but can't
// because, it is different project.
@Entity
class A{
@OneToMany
B b;
}
//in B microservice
@Entity
class B{
}
However, due to microservice design, they all are in different packages or services. Should I create another project as a shared library including all entities inside of it, then include in every microservice? Do you have another solution, or is this a solution?
I have found the solution after long research. I am sharing these links to help others looking this question later.
Basically, we need to add a column for foreign key. For my example in question, We need to add b_id instead of B object. Then we set B's id. When we need B, we will fetch it by using that id.
https://www.quora.com/How-do-I-handle-Foreign-Keys-with-a-Microservices-architecture
http://microservices.io/patterns/data/database-per-service.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With