hibernate4之一对多自身映射
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate4之一对多自身映射相关的知识,希望对你有一定的参考价值。
示例:部门下有部门(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
以上是关于hibernate4之一对多自身映射的主要内容,如果未能解决你的问题,请参考以下文章