Search code examples
jhipsterjdl

JHipster JDL: how to link entities (by ID) among microservices?


I am running the latest JHipster generator

izio@1z10:~$ jhipster --version
Using JHipster version installed globally
5.3.4

and I'm using the following JDL by running

jhipster import-jdl jhipster-jdl.jh

to generate my microservices and gateway projects.

application {
  config {
    baseName Gateway
    applicationType gateway
    packageName com.app.gateway
    databaseType postgresql
    devDatabaseType postgresql
    prodDatabaseType postgresql
    serverPort 8080
    languages [en,it,de,fr,es,pt-pt]
    serviceDiscoveryType eureka
  }
  entities *
}
application {
  config {
    baseName authorMS
    applicationType microservice
    packageName com.app.ams
    databaseType postgresql
    devDatabaseType postgresql
    prodDatabaseType postgresql
    serverPort 8081
    serviceDiscoveryType eureka
  }
  entities Author
}

application {
  config {
    baseName bookMS
    applicationType microservice
    packageName com.app.bms
    databaseType postgresql
    devDatabaseType postgresql
    prodDatabaseType postgresql
    serverPort 8082
    serviceDiscoveryType eureka
  }
  entities Book
}

entity Author{
    Name String required
}
entity Book{
    Name String required
}

relationship ManyToMany{
    Book{authors(name)} to Author
}

dto * with mapstruct
service * with serviceImpl
paginate * with pagination

microservice Author with authorMS
microservice Book with bookMS

Everything seems to be ok, at least with the generation part, since there is no error with the jhipster import-jdl command. The prolem here is that I need a way to link entities by their ID among different microservices. Right now the link is made wrongly to the other entity class (even if it resides in a different microservice). Obviously this results in an impossible to run app, due to missing entity class.

In such cases the only logical solution would be to link entities among different microservices by using their IDs rather than the entity class.

Is there any way to do this from the JDL, rather than making the required changes by hand?


Solution

  • You're right specifying a relationship between entities from different microservices does not work and I think the JDL parser should raise an error in such case (feel free to report an issue on github), I did not try but I think the entity sub generator does not allow it. So you could define simple id fields in your entities but it will not be a relationship because it won't allow you to easily build a JOIN queries in database as each entity reside in a different database and the JHipster will not generate frontend code for it too.

    I suggest also that you re-consider how you split your business domain amongst microservices. Building such relationships is an anti pattern in microservices architecture, each microservices should be defined around bounded contexts (refer to Domain Driven Design for details). If 2 entities have a strong relationship, it often means they should belong to same microservice. There can be exceptions but using inter-service calls to join entities is very expensive and fragile compared to JOIN database queries within one microservice, so you should avoid them.