我试图保留一个具有内部列表的对象。
我必须使用@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);
}
}
在REST控制器中:
@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;
如果您想知道orphanRemoval有什么用,请查看此question。