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


    <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>
    

      

  • 相关阅读:
    生成唯一流水码
    搜索类
    数字转中文
    字符串转数组工具类
    类转换
    P1112 区间连续段
    P1113 同颜色询问
    Turtlebot3 机器学习
    Turtlebot2进阶教程
    turtlebot A2
  • 原文地址:https://www.cnblogs.com/jifeng/p/4420864.html
Copyright © 2020-2023  润新知