Parent-child relationship processing

Stocktaking learned (a)

The project met a situation. Figure:

This is a dynamic form, you can add data rows. Wherein the data format which may exist Json one. That is the relationship between father and son-class existence.

First, referring to the next: this type of data, the corresponding multiple tables in a database is not necessary, since a single table can be associated.

Next to the focus of this paper, how to save such data structures in a database. Corresponding business scenario, there are two:

One is the real-time save, save after adding a row of data in real-time data return newly inserted (it is mainly primary key id). To do so, when it comes to parent-child class, you can first add the parent, but the parent of return information, which the child can be given that is parentId rows of data. It can achieve save his son-level data;

The second is saved only once, this time the father and son at the same time passed the background level is warehousing. It does not specify when the parent id for the sub-level data, the direct saving can not be achieved. I would like a way to save is to this most important of all the things a big mess directly Json string of children, both Json save the data and save the relationship between the data. I like this backend can resolve their own use. But it more than a layer of complexity, not good, not good.

Old driver told me: You can receive data add a data object list List in data transfer object, the child so the child would have to keep an object in the

Specifically: a row of data above corresponds to a LogAnalysisConfigDtoclass, I am now designing a LogAnalysisConfigCustomDtoreinforced class, inherited from LogAnalysisConfigDtoand add attributes private List<LogAnalysisConfigDto> analysisConfigDtoList;. Like this entire class structure:

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * @author kangjia
 * @date 2019/11/4 15:52
 */
@Data
@Accessors(chain = true)
@ApiModel(value = "日志分析配置增强类", description = "日志分析配置增强类")
public class LogAnalysisConfigCustomDto extends LogAnalysisConfigDto {

    /**
     *  子级关系
     */
    private List<LogAnalysisConfigDto> analysisConfigDtoList;
}

Designed this way, when the rear end of the stored list is determined whether the attribute is null, two conditions can be put in storage.

Guess you like

Origin www.cnblogs.com/kjgym/p/11798914.html