hibernate注解配置一对多,分类表

hibernate注解配置一对多,分类表,使用parentId指向Id关联。查询的结果为树型。
折腾一天,一直失败。差点把自己玩死,最主要一点就是: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;
}




posted on 2013-12-08 00:17  仗帅闯江湖  阅读(45)  评论(0编辑  收藏  举报

导航