简体   繁体   中英

What is the best practice to implement oneToMany relationship?

I'm developing a rest API that provides an endpoint to insert Rules and Items. I have an entity named rule and another named item like bellow:

Rule.java

@Getter
@Setter
@Entity
@Table(name = "rule")
public class Rule extends PhysicalBaseEntity {

    @NotNull
    @Column(name = "title")
    @Size(max = 100)
    private String title;

    @NotNull
    @Column(name = "description")
    @Size(max = 150)
    private String description;

    @OneToMany(cascade = {CascadeType.MERGE}, orphanRemoval = true)
    @JoinColumn(name = "rule_id", nullable = false)
    private Set<Item> items;

}

Item.java

@Getter
@Setter
@Entity
@Table(name = "item")
public class Item extends PhysicalBaseEntity {

    @NotNull
    @Column(name = "code")
    @Size(max = 50)
    private String code;

    @NotNull
    @Column(name = "description")
    @Size(max = 150)
    private String description;

    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    private Item parent;

    @OneToMany(mappedBy="parent", cascade={CascadeType.ALL})
    private Set<Item> children;

}

and I need to use a VO entity to represent that information, but I don't know how I ll create this VO's and I don t know if this approach is the best. My first idea is the Rule entity send all information, like this:

{
    "id": "55cfd101-3bc3-4842-a1ab-f88096b9ea06",
    "title": "TITLE",
    "description": "DESCRIPTION",
    "items": [
        {
            "id": "fa1c8e5f-5791-491b-b1ee-4d09048608d6",
            "code": "2",
            "description": "PARENT",
            "children": [
                {
                    "id": "755bed57-1581-4524-bae1-4ec648711a88",
                    "code": "000",
                    "description": "FIRST SON"
                    "children": [
                        {
                            "id": "1420ad3b-5278-48fe-a638-77a2e16feb39",
                            "code": "111",
                            "description": "SECOND SON"
                            "children": [
                                {
                                    "id": "f55cf8d1-b2e4-4a9e-9f79-df156a5ac0a4",
                                    "code": "222",
                                    "description": "THIRD SON"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

Is this the best idea for me?

Everything depends on your requirements and situation. You should understand the next things:

  1. If something will change in your entities - will client be updated the same time? If so, you can use your entity as DTO, but it's a bad practice, cause you need to worry about client-side too. Or you can add to your entities some @JsonProperty / @JsonIgnore(ignoreUnknown=true) annotations, to secure yourself from fields' names mismatch or situations, when client sent you some property, which is not in entity.

  2. If client independent and won't be changed if eg you'll add new field to entity - you can create DTOs which look simalar to entity, except JPA annotations and use them. Write some utils mapper, who will take data from DTO, transform them into entities and call save() method (or whatever you wanna do with your entities). But in your case, cause you have some kind of tree structure - such mapper will be a little bit more complicated.

If I were you - i'd better stuck with option 2. It's more complicated, requires a little bit more code, but in future it will be more scalable, flexible and easy to support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM