Hibernate Annotation 配置自身OneToMany
使用部门表SYS_DEPARTMENT进行测试
其中parent_id用于记录你部门ID
部门类
package pineapple.web.pal.model;import java.io.Serializable;import java.util.HashSet;import javax.persistence.Basic;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;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.SequenceGenerator;import javax.persistence.Table;import org.hibernate.annotations.Cascade;import org.hibernate.annotations.CascadeType;/** */@Entity@Table(schema = "TRAIN", name = "SYS_DEPARTMENT")public class Department implements Serializable {private static final long serialVersionUID = 1L;@Id @SequenceGenerator(name = "SEQ_ROLE", sequenceName = "SEQ_ROLE")@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "SEQ_ROLE")private Integer id;@Column(name = "VERSION")private Integer version;@Column(name = "NAME", length = 100, nullable = false)private String name;@Column(name = "DESCRIPTION")private String description;@Column(name = "ENABLED")@Basic(fetch = FetchType.EAGER)private Integer enabled;@Column(name = "CODE")private String code;@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE})@ManyToOne(fetch=FetchType.LAZY)@JoinColumn(name="PARENT_ID", referencedColumnName="ID")private Department parent;@Cascade(value={CascadeType.SAVE_UPDATE})@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)private java.util.Set<Department> children;