折腾一天,一直失败。差点把自己玩死,最主要一点就是:parentId为外键,需要先手动在数据库增加一条记录,作为root,然后才能使用hibernate做CRUD操作。
@javax.persistence.Entity
@javax.persistence.Table(name = "category")
public class Category {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Id")
private int id;
@Column(name="ParentId",nullable=true)
private int parentId;
private String Name;
@Column(name="Display")
private String text;
private String Remark;
@Column(columnDefinition="bit")
private boolean Deleted;
@Column(nullable=true,insertable=false,updatable=false)
private int AddUserId;
private Date AddDate;
@Column(nullable=true,insertable=false,updatable=false)
private int ModUserId;
private Date ModDate;
@OneToMany()
@JoinColumn(name="id", referencedColumnName="parentId")
private Set<Category> children;
@ManyToOne(targetEntity=SysUser.class)
@JoinColumn(name="AddUserId")
private SysUser AddUser;
@ManyToOne(targetEntity=SysUser.class)
@JoinColumn(name="ModUserId")
private SysUser ModUser;
}