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
}


你可能感兴趣的:(hibernate4之一对多自身映射(四))