Save Long Associations
What is Long Association
A long association refers to not only changing the association relationship between the current object and other objects, but also further modifying the associated object.
For the UI interface, it is usually manifested as nested parent-child forms (or even recursive trees). The forms are diverse, and sub-forms embedded in forms are quite common, as follows:
As long as developers are willing, the depth of this nested parent-child form structure can be more than two layers. In theory, the depth can be infinite, which is why it is called a long association.
Write DTO
In the current example in the entity definition, the more suitable example as a long association to save is the BookStore
and its associated books
collection.
-
Install DTO language Intellij plugin: https://github.com/ClearPlume/jimmer-dto (This process is not required but highly recommended)
-
Create a new directory
src/main/dto
-
Under
src/main/dto
, create a fileBookStore.dto
and write the following code:BookStore.dtoexport org.doc.j.model.BookStore
-> package org.doc.j.model.dto
input BookStoreWithLongAssociation {
#allScalars(this)
books { // LongAssociation
#allScalars(this)
-id
id(authors) as authorIds
}
}
Generated Code
After compilation, Jimmer will generate the following code:
- Java
- Kotlin
@GeneratedBy(
file = "<yourproject>/src/main/dto/BookStore.dto"
)
public class BookStoreWithLongAssociation implements Input<BookStore> {
@Nullable
private Long id;
@NotNull
private String name;
@Nullable
private String website;
@NotNull
private List<TargetOf_books> books;
...Omit other methods...
public static class TargetOf_books implements Input<Book> {
@NotNull
private String name;
private int edition;
@NotNull
private BigDecimal price;
@NotNull
private List<Long> authorIds;
...Omit other methods...
}
}
@GeneratedBy(
file = "<yourproject>/src/main/dto/BookStore.dto"
)
data class BookStoreWithLongAssociation(
val id: Long?
val name: String,
val website: String?,
val books: List<TargetOf_books>
) : Input<BookStore> {
...Omit other methods...
data class TargetOf_authors(
val name: String,
val edition: Int,
val price: BigDecimal,
val authorIds: List<Long>
)
}
Write HTTP Service
- Java
- Kotlin
@RestController
public class BookStoreController {
private final JSqlClient sqlClient;
public BookController(JSqlClient sqlClient) {
this.sqlClient = sqlClient;
}
@PutMapping("/bookStore")
public int saveBookStoreWithLongAssociation(
@RequestBody BookStoreWithLongAssociation input
) {
return sqlClient
.save(input)
.getTotalAffectedRowCount();
}
}
class BookStoreController(
private val sqlClient: KSqlClient
) {
@PutMapping("/bookStore")
fun saveBookStoreWithLongAssociation(
@RequestBody input: BookStoreWithLongAssociation
): Int =
sqlClient
.save(input)
.totalAffectedRowCount
}
It can be seen that no matter how the Input DTO changes, Jimmer still only needs one method call to complete data saving.