Remote Association
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 defaultmicroServiceName
, 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
- Java
- Kotlin
Book.java@Entity(microServiceName = "book-service")
public interface Book {
@ManyToMany
List<Author> authors();
...Omit other code...
}Book.kt@Entity(microServiceName = "book-service")
interface Book {
@ManyToMany
val authors: List<Author>
...Omit other code...
} -
Inverse side (optional):
Author.authors
- Java
- Kotlin
Author.java@Entity(microServiceName = "author-service")
public interface Author {
@ManyToMany(mappedBy = "authors")
List<Book> books();
...Omit other code...
}Author.kt@Entity(microServiceName = "author-service")
interface Author {
@ManyToMany(mappedBy = "authors")
val books: List<Book>
...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
- Java
- Kotlin
BookServiceCommonEntity.java@MappedSuperclass(microServiceName = "book-service")
public interface BookServiceCommonEntity {
LocalDateTime createdTime();
@ManyToOne
User createdBy();
LocalDateTime modifiedTime();
@ManyToOne
User modifiedBy();
}BookServiceCommonEntity.kt@MappedSuperclass(microServiceName = "book-service")
interface BookServiceCommonEntity {
val createdTime: LocalDateTime
@ManyToOne
val createdBy: User
val modifiedTime: LocalDateTime
@ManyToOne
val modifiedBy: User
}Here
BookServiceCommonEntity
contains association propertiescreatedBy
andmodifiedBy
, but onlyEntity
or otherMappedSuperclass
belonging to the microservice "book-service" can inherit it. -
Can span any microservices, but must not contain association properties
- Java
- Kotlin
CommonEntity.java@MappedSuperclass(acrossMicroServices = true)
public interface CommonEntity {
LocalDateTime createdTime();
LocalDateTime modifiedTime();
}CommonEntity.kt@MappedSuperclass(acrossMicroServices = true)
interface CommonEntity {
val createdTime: LocalDateTime
val modifiedTime: LocalDateTime
}Here
CommonEntity
can span any microservices. AnyEntity
or otherMappedSuperclass
under any microservice can inherit it, but it itself does not contain any association properties.