选择时,Hibernate重复子数组

时间:2016-06-24 13:13:42

标签: java hibernate spring-boot

我遇到了Hibernate的问题,并选择了与同一个表中其他行相关的行。我得到了正确的结果,但我得到了两个相同数据的数组。

例如,我得到了这个:

[
  {
    "id": 1,
    "category": "Banner",
    "childCategories": [
      {
        "id": 11,
        "category": "Flash Banner",
        "childCategories": [],
        "children": []
      },
      {
        "id": 3,
        "category": "Image Banner",
        "childCategories": [],
        "children": []
      }
    ],
    "children": [
      {
        "id": 11,
        "category": "Flash Banner",
        "childCategories": [],
        "children": []
      },
      {
        "id": 3,
        "category": "Image Banner",
        "childCategories": [],
        "children": []
      }
    ]
  }
]

当我期待这个时:

[
  {
    "id": 1,
    "category": "Banner",
    "childCategories": [
      {
        "id": 11,
        "category": "Flash Banner",
        "childCategories": [],
        "children": []
      },
      {
        "id": 3,
        "category": "Image Banner",
        "childCategories": [],
        "children": []
      }
    ]
  }
]

以下是我的实体中设置与父行的关系并将结果添加到childCategories属性的内容。

@Entity
@Table(name = "adMediaCategory")
public class AdMediaCategory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String category;

    @ManyToOne(cascade = { CascadeType.ALL })
    @JoinColumn(name = "parentId")
    @JsonBackReference
    private AdMediaCategory parent;

    @OneToMany(mappedBy = "parent")
    @JsonManagedReference
    private Set<AdMediaCategory> childCategories = new HashSet<>();

    public Integer getId() {
    `enter code here`return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public AdMediaCategory getParent() {
        return this.parent;
    }

    public void setParent(AdMediaCategory parentCategory) {
        this.parent = parentCategory;
    }

    public Set<AdMediaCategory> getChildren() {
        return this.childCategories;
    }

    public void setChildren(Set<AdMediaCategory> children) {
        this.childCategories = children;
    }
}

任何人都可以看到这些注释的问题吗?

提前致谢!

0 个答案:

没有答案