树状结构

import static javax.persistence.CascadeType.REMOVE;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonManagedReference;

/**
 * The persistent class for the sys_tree database table.
 *
 */
/**
 * @author lee
 *
 */
@Entity
@Table(name = "sys_tree")
public class SysTree implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private String name;

	// bi-directional many-to-one association to SysTree
	@ManyToOne
	@JoinColumn(name = "parent_id")
	@JsonBackReference
	private SysTree parentNode;

	// bi-directional many-to-one association to SysTree
	@OneToMany(cascade = REMOVE)
	@JsonManagedReference
	@JoinColumn(name = "parent_id", referencedColumnName = "id")
	private List<SysTree> childNodes;

	/**
	 *
	 */
	public SysTree() {
	}

	public Long getId() {
		return this.id;
	}

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

	public SysTree getParentNode() {
		return this.parentNode;
	}

	public void setParentNode(SysTree parentNode) {
		this.parentNode = parentNode;
	}

	public List<SysTree> getChildNodes() {
		return this.childNodes;
	}

	public void setChildNodes(List<SysTree> childNodes) {
		this.childNodes = childNodes;
	}

	public SysTree addChildNodes(SysTree childNodes) {
		getChildNodes().add(childNodes);
		childNodes.setParentNode(this);

		return childNodes;
	}

	public SysTree removeChildNodes(SysTree childNodes) {
		getChildNodes().remove(childNodes);
		childNodes.setParentNode(null);

		return childNodes;
	}



	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("SysTree [id=");
		builder.append(id);
		builder.append(", name=");
		builder.append(name);
		builder.append(", parentNode=");
		builder.append(parentNode==null?"":parentNode.id);
		builder.append("]");
		return builder.toString();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}



你可能感兴趣的:(树状结构)