hibernate 自生双向一对多 多对一管理 (树)

时间:2023-02-06 17:19:50
<span style="font-size: large;">package com.javacrazyer.test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version; /**
* 产品类别
*
*/
@Entity
@Table(name="category")
public class Category implements Serializable{
private static final long serialVersionUID = -3942148673242309324L;
@Id
@GeneratedValue
private Integer id;
@Version
private Integer version;
private String name;
private String description;
private String path; //分类路径 //Category 自身双向多对一
@ManyToOne(cascade={CascadeType.MERGE,CascadeType.PERSIST})
private Category parent; //Category 自身双向一对多
@OneToMany(mappedBy="parent",fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name="parent_id")
private List<Category> child = new ArrayList<Category>(); public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Category getParent() {
return parent;
}
public void setParent(Category parent) {
this.parent = parent;
}
public List<Category> getChild() {
return child;
}
public void setChild(List<Category> child) {
this.child = child;
}
public Integer getVersion() {
return version;
} @SuppressWarnings("unused")
private void setVersion(Integer version) {
this.version = version;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}</span>