Skip to main content

Remote Association

caution

Remote association is a product of combining Jimmer with microservice architecture. It will be introduced in detail in Spring Cloud and Remote Association.

The purpose of this article is not to systematically explain remote associations, just to introduce the mapping configuration required for remote associations.

@Entity

The @Entity annotation has an optional parameter microServiceName whose default value is "".

Association properties involve two entity types. Since associations must point from the source to the target (self association properties are special, the source and target overlap):

  • If the microServiceName of the source entity and target entity of the association property are equal, then this association is a local association (All entities in the examples before this article use the default microServiceName, so they are local associations).

  • If the microServiceName of the source entity and target entity of the association property are not equal, then this association is a remote association.

For example:

  • Owning side (required): Book.authors

    Book.java
    @Entity(microServiceName = "book-service")
    public interface Book {

    @ManyToMany
    List<Author> authors();

    ...Omit other code...
    }
  • Inverse side (optional): Author.authors

    Author.java
    @Entity(microServiceName = "author-service")
    public interface Author {

    @ManyToMany(mappedBy = "authors")
    List<Book> books();

    ...Omit other code...
    }

Here, the microservice name of Book is book-service, and the microservice name of Author is author-service. The two are not equal, so Book.authors and Author.books are remote associations.

@MappedSuperclass

As a super type, MappedSuperclass has two usages:

  • Can contain association properties, but must belong to the same microservice

    BookServiceCommonEnity.java
    @MappedSuperclass(microServiceName = "book-service")
    public interface BookServiceCommonEnity {

    LocalDateTime createdTime();

    @ManyToOne
    User createdBy();

    LocalDateTime modifiedTime();

    @ManyToOne
    User modifiedBy();
    }

    Here BookServiceCommonEnity contains association properties createdBy and modifiedBy, but only Entity or other MappedSuperclass belonging to the microservice "book-service" can inherit it.

  • Can span any microservices, but must not contain association properties

    CommonEnity.java
    @MappedSuperclass(acrossMicroServices = true)
    public interface CommonEnity {

    LocalDateTime createdTime();

    LocalDateTime modifiedTime();
    }

    Here CommonEnity can span any microservices. Any Entity or other MappedSuperclass under any microservice can inherit it, but it itself does not contain any association properties.