我正在使用Spring框架创建一个简单的REST服务器。 其中一个类需要自己加入一对多关系来检索有关它的父对象的数据。这堂课看起来像这样;
@Entity
@Table(name = "item", schema = "tmp", uniqueConstraints = {
@UniqueConstraint(columnNames = "item_id"), @UniqueConstraint(columnNames = "item_name")})
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private Integer itemId;
private String itemName;
private String itemDescription;
private Item itemParent;
private boolean isEndItem;
private List<Item> childrenItems;
public Item() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_id", unique = true, nullable = false)
public Integer getItemId() {
return itemId;
}
public void setItemId(final Integer itemId) {
this.itemId = itemId;
}
@Column(name = "item_name", unique = true, nullable = false)
public String getItemName() {
return itemName;
}
public void setItemName(final String itemName) {
this.itemName = itemName;
}
@Column(name = "item_description", unique = false, nullable = false)
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(final String itemDescription) {
this.itemDescription = itemDescription;
}
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonManagedReference(value = "item-item")
@JoinColumn(name="item_parentid")
public Item getParentItem() {
return this.itemParent;
}
public void setParentItem(final Item itemParent){
this.itemParent = itemParent;
}
@OneToMany(mappedBy="childrenItems", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonBackReference(value = "item-item")
public List<Item> getChildrenItems(){
return this.childrenItems;
}
public void setChildrenItems(final List<Item> childrenItems){
this.childrenItems = childrenItems;
}
@Column(name = "item_isendproduct", unique = false, nullable = false)
public boolean isEndItem() {
return isEndItem;
}
public void setEndItem(final boolean isEndItem) {
this.isEndItem = isEndItem;
}
}
在我的数据库中,测试数据保存在此层次结构中:
条目&gt;沙拉&gt; House Salad
[
{
"itemId":1,
"itemName":"Entry",
"itemDescription":"Entry Menu",
"endItem":false,
"parentItem":null
},
{
"itemId":2,
"itemName":"Salad",
"itemDescription":"Salad Menu",
"endItem":false,
"parentItem":{
"itemId":1,
"itemName":"Entry",
"itemDescription":"Entry Menu",
"endItem":false,
"parentItem":null
}
},
{
"itemId":3,
"itemName":"House Salad",
"itemDescription":"House Salad",
"endItem":true,
"parentItem":{
"itemId":2,
"itemName":"Salad",
"itemDescription":"Salad Menu",
"endItem":false,
"parentItem":{
"itemId":1,
"itemName":"Entry",
"itemDescription":"Entry Menu",
"endItem":false,
"parentItem":null
}
}
}
]
但我不想知道有关父母对象的信息。 是否有任何注释可用于实现此目的?
提前感谢