示例:部门下有部门(Department )
public class Department { private Integer id; private String name; private Set<Department> departments; private Department department; //set get }
用XML映射
<hibernate-mapping package="com.shen.hibernate4.entity"> <class name="Department " table="tb_department "> <id name="id"> <generator class="identity" /> </id> <property name="name" /> <many-to-one name="department" class="Department" column="parent_id" /> <set name="departments" inverse="true" cascade="all"> <key column="parent_id" /> <one-to-many class="Department" /> </set> </class> </hibernate-mapping>
用@Annotation映射
@Entity @Table(name = "tb_department") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; @OneToMany(mappedBy = "department", targetEntity = Department.class, cascade = CascadeType.ALL) private Set<Department> departments; @ManyToOne @JoinColumn(name = "parent_id") private Department department; //set get }
本文出自 “冰葫芦” 博客,请务必保留此出处http://shenlang7.blog.51cto.com/3102414/1742812
原文地址:http://shenlang7.blog.51cto.com/3102414/1742812