java - Hibernate 省略 JsonManagedReference 来持久化

标签 java json spring hibernate jpa

我正在尝试保留一个具有内部列表的对象。 我必须用 @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/

相关文章:

java - 父类(super class)对子类对象的引用显示与子类对子类对象的引用相同的行为

java - 如何将对象从注册表传输到 Controller 以写入数据库并分配角色?

java - 您如何将 JSON 补丁与 Spring Data REST 结合使用?

java - 在 Java 中动态链接比较器

java - Google OAuth Credential.refreshToken 上的连接重置

java - 警报+表单+j2me

json - list 不是有效的 JSON。行 : 1, 列 : 1, 意外 token

javascript - Angular JS在textarea中显示和编辑模型

java - JQuery AJAX Done 未执行

spring - GWT - RemoteService 接口(interface)和 Spring - 如何获取 HttpSession?