码迷,mamicode.com
首页 > Web开发 > 详细

hibernate4之一对多自身映射(四)

时间:2016-02-17 22:37:08      阅读:202      评论:0      收藏:0      [点我收藏+]

标签: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之一对多自身映射(四)

标签:hibernate4之一对多自身映射

原文地址:http://shenlang7.blog.51cto.com/3102414/1742812

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!