gpt4 book ai didi

java - Hibernate 省略 JsonManagedReference 来持久化

转载 作者:行者123 更新时间:2023-12-02 10:18:59 26 4
gpt4 key购买 nike

我正在尝试保留一个具有内部列表的对象。我必须用 @JsonManagedReference 注释实体 Item和 ItemProperty 与 @JsonBackReference,以避免无限循环 - 打破循环。

对于获取具有项目属性的项目来说是可以的。现在的问题是,当我尝试使用项目属性列表保留新项目时,则仅保留该项目,而没有任何 ItemProperties。有谁知道这是为什么吗? @JsonBackReference/ManagedReference 注释有什么用吗?

代码:

import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "item")
public class Item {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Enumerated(EnumType.STRING)
@Column(name = "type")
private ItemType itemType;

@OneToMany(mappedBy = "item")
// @JsonManagedReference is the forward part of reference which gets serialized normally.
@JsonManagedReference
private List<ItemProperty> itemProperties;

public Item() {

}

public Item(ItemType itemType, List<ItemProperty> itemProperties) {
this.itemType = itemType;
this.itemProperties = itemProperties;
}

public int getId() {
return id;
}

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

public ItemType getItemType() {
return itemType;
}

public void setItemType(ItemType itemType) {
this.itemType = itemType;
}

public List<ItemProperty> getItemProperties() {
return itemProperties;
}

public void setItemProperties(List<ItemProperty> itemProperties) {
this.itemProperties = itemProperties;
}

@Override
public String toString() {
return "Item{" +
"id=" + id +
", itemType=" + itemType +
", itemProperties=" + itemProperties +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return id == item.id &&
itemType == item.itemType &&
Objects.equals(itemProperties, item.itemProperties);
}

@Override
public int hashCode() {
return Objects.hash(id, itemType, itemProperties);
}
}

元素属性:

import com.fasterxml.jackson.annotation.JsonBackReference;

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "item_properties")
public class ItemProperty {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "item_id")
@JsonBackReference
private Item item;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "item_property_definition_id")
private ItemPropertyDefinition itemPropertyDefinition;

@Column(name = "value")
private String value;

public ItemProperty(){}

public ItemProperty(Item item, ItemPropertyDefinition itemPropertyDefinition, String value) {
this.item = item;
this.itemPropertyDefinition = itemPropertyDefinition;
this.value = value;
}

public int getId() {
return id;
}

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

public Item getItem() {
return item;
}

public void setItem(Item item) {
this.item = item;
}

public ItemPropertyDefinition getItemPropertyDefinition() {
return itemPropertyDefinition;
}

public void setItemPropertyDefinition(ItemPropertyDefinition itemPropertyDefinition) {
this.itemPropertyDefinition = itemPropertyDefinition;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public String toString() {
return "ItemProperty{" +
"id=" + id +
", item=" + item +
", itemPropertyDefinition=" + itemPropertyDefinition +
", value='" + value + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemProperty that = (ItemProperty) o;
return id == that.id &&
Objects.equals(item, that.item) &&
Objects.equals(itemPropertyDefinition, that.itemPropertyDefinition) &&
Objects.equals(value, that.value);
}

@Override
public int hashCode() {
return Objects.hash(id, item, itemPropertyDefinition, value);
}

}

在休息 Controller 中:

   @PostMapping("/items")
Item addItem(@RequestBody Item item) {
item.setId(0);
return this.itemService.addItem(item);
}

提前感谢您的提示。

祝你有美好的一天,快乐的编码!

最佳答案

您尚未在 @OneToMany 中声明级联标志。默认情况下,对项实体的操作不会级联到 ItemProperty 列表。因此,请查看 CascadeType 枚举并将要级联到 itemsproperty 列表的操作设置为。有关 CascadeTypes 的更多信息,请查看 here .

示例:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "item", orphanRemoval = true)
// @JsonManagedReference is the forward part of reference which gets serialized normally.
@JsonManagedReference
private List<ItemProperty> itemProperties;

如果你想知道孤儿移除有什么用,请看一下这个 question .

关于java - Hibernate 省略 JsonManagedReference 来持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54474767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com