繁体   English   中英

Spring Boot Data JPA 中的自加入 - 仅显示第一个子节点(第 0 个元素)而不是所有子节点

[英]Self Join in Spring Boot Data JPA - Showing only the first child node (0th element) not all children nodes

我想在应用程序中使用的用例是我有一个列表,列表将有很多孩子,孩子们会有孩子等等。 请参考下图。

在此处输入图片说明

为了实现这种层次结构,我创建了以下 Java 类;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Table(name = "ITEM")
public class Item implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @Id
    @JsonIgnore
    @Column(name = "ID", updatable = false, nullable = false, unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    
    @Size(max = 500)
    @Column(name = "TYPE", nullable = false, length = 500)
    private String type;
    
    @Column(name = "READONLY")
    private Boolean readOnly;
    
    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "PARENT")
    private Item parent;
    
    @JsonManagedReference
    @Builder.Default
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval=true, fetch = FetchType.LAZY)
    private Set<Item> children = new HashSet<Item>();

}

插入工作正常。 但是当我检索数据时,我没有在Set<Item> children节点中获取父节点下的所有子节点,而是只获取第一个子节点,即; 集合中的第 0 个元素。 我不知道这里的问题,我做错了什么。 我得到如下输出;

  "items": [
    {
      "id": "1",
      "type": "PARENT 1",
      "parent": null,
      "readOnly": false,
      "children": [
        {
          "id": "6",
          "type": "CHILDREN 1",
          "parent": 1,
          "readOnly": false,
          "children": []
        }
      ]
    },
    {
      "id": "2",
      "type": " PARENT 2",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "3",
      "type": " PARENT 3",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "4",
      "type": " PARENT 4",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "5",
      "type": " PARENT 5",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "6",
      "type": "CHILDREN 1",
      "parent": 1,
      "readOnly": false,
      "children": []
    },
    {
      "id": "7",
      "type": "CHILDREN 1",
      "parent": 1,
      "readOnly": false,
      "children": []
    }
  ]
}

我应该在哪里得到这样的输出;

  "items": [
    {
      "id": "1",
      "type": "PARENT 1",
      "parent": null,
      "readOnly": false,
      "children": [
        {
          "id": "6",
          "type": "CHILDREN 1",
          "parent": 1,
          "readOnly": false,
          "children": []
        },
        {
          "id": "7",
          "type": "CHILDREN 1",
          "parent": 1,
          "readOnly": false,
          "children": []
        }
      ]
    },
    {
      "id": "2",
      "type": " PARENT 2",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "3",
      "type": " PARENT 3",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "4",
      "type": " PARENT 4",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "5",
      "type": " PARENT 5",
      "parent": null,
      "readOnly": false,
      "children": []
    },
    {
      "id": "6",
      "type": "CHILDREN 1",
      "parent": 1,
      "readOnly": false,
      "children": []
    },
    {
      "id": "7",
      "type": "CHILDREN 1",
      "parent": 1,
      "readOnly": false,
      "children": []
    }
  ]
}

在第一项中,我应该得到 2 个孩子,而是得到一个孩子元素。

您的问题可能与Item类的equals实现有关:您正在使用@EqualsAndHashCode(onlyExplicitlyIncluded = true)指示 Lombok 仅包含您希望在equals实现中的字段或方法,但您没有使用@EqualsAndHashCode.Include 请参阅相关文档

由于这个原因,Lombok 可能正在生成一个equals实现,它起源于每个Item被认为与另一个相等,因此children Set只包含有关添加的第一个Item信息。

请使用@EqualsAndHashCode.Include注释您认为合适的字段,我认为它可以解决问题。 例如:

//...
@EqualsAndHashCode.Include
private Long id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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