Skip to main content

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:

CommodityQuantityUnit priceItem priceDelete
14.6929.38
3030
Total price:59.379999999999995
info

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.

  1. Install DTO language Intellij plugin: https://github.com/ClearPlume/jimmer-dto (This process is not required but highly recommended)

  2. Create a new directory src/main/dto

  3. Under src/main/dto, create a file BookStore.dto and write the following code:

    BookStore.dto
    export 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:

BookStoreWithLongAssociation.java
@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...
}
}

Write HTTP Service

BookStoreController.java
@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();
}
}

It can be seen that no matter how the Input DTO changes, Jimmer still only needs one method call to complete data saving.