zoukankan      html  css  js  c++  java
  • hibernate一对多关系映射(自身关联)

    示例:一个类别(Category)下面有多个子类别,多个子类别属于同一个父类别。

    Java代码  收藏代码
    1. public class Category  {  
    2.   
    3.     private Integer id;  
    4.     private String name;  
    5.     private Category parentCategory; // 父类别  
    6.     private Set<Category> childCategories = new HashSet<Category>(); // 子类别  
    7.   
    8.     // getter and setter  
    9. }  

     用XML映射

    Xml代码  收藏代码
    1. <hibernate-mapping package="org.monday.hibernate4.domain">  
    2.     <class name="Category" table="tbl_category">  
    3.         <id name="id">  
    4.             <generator class="identity" />  
    5.         </id>  
    6.         <property name="name" />  
    7.         <many-to-one name="parentCategory" class="Category" column="category_id" />  
    8.         <set name="childCategories" inverse="true" cascade="all">  
    9.             <key column="category_id" />  
    10.             <one-to-many class="Category" />  
    11.         </set>  
    12.     </class>  
    13. </hibernate-mapping>  

     用@Annotation映射

    Java代码  收藏代码
    1. @Entity  
    2. @Table(name = "tbl_category")  
    3. public class Category  {  
    4.   
    5.     @Id  
    6.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
    7.     private Integer id;  
    8.     private String name;  
    9.   
    10.     @ManyToOne  
    11.     @JoinColumn(name = "category_id")  
    12.     private Category parentCategory; // 父类别  
    13.   
    14.     @OneToMany(mappedBy = "parentCategory", targetEntity = Category.class, cascade = CascadeType.ALL)  
    15.     private Set<Category> childCategories = new HashSet<Category>(); // 子类别  
    16.   
    17.     //getter and setter  
    18. }  

     

    测试代码

    Java代码  收藏代码
    1.                      Category foodCategory = new Category("food");  
    2. Category fruitCategory = new Category("fruit");  
    3. Category vegetableCategory = new Category("vegetable");  
    4. Category appleCategory = new Category("apple");  
    5. Category orangeCategory = new Category("orange");  
    6. Category tomatoCategory = new Category("tomato");  
    7.   
    8. // 建立食品类别和水果类别之间的关联关系  
    9. foodCategory.getChildCategories().add(fruitCategory);  
    10. fruitCategory.setParentCategory(foodCategory);  
    11.   
    12. // 建立食品类别和蔬菜类别之间的关联关系  
    13. foodCategory.getChildCategories().add(vegetableCategory);  
    14. vegetableCategory.setParentCategory(foodCategory);  
    15.   
    16. // 建立水果类别和苹果类别之间的关联关系  
    17. fruitCategory.getChildCategories().add(appleCategory);  
    18. appleCategory.setParentCategory(fruitCategory);  
    19.   
    20. // 建立水果类别和桔子类别之间的关联关系  
    21. fruitCategory.getChildCategories().add(orangeCategory);  
    22. orangeCategory.setParentCategory(fruitCategory);  
    23.   
    24. // 建立西红柿类别和蔬菜类别之间的关联关系  
    25. tomatoCategory.setParentCategory(vegetableCategory);  
    26. vegetableCategory.getChildCategories().add(tomatoCategory);  
    27.   
    28. session.save(foodCategory);  

     SQL schema

    Sql代码  收藏代码
    1. Hibernate:   
    2.     create table CATEGORYS (  
    3.         id integer not null,  
    4.         name varchar(255),  
    5.         category_id bigint,  
    6.         primary key (id)  
    7.     )  
    8. Hibernate:   
    9.     alter table CATEGORYS   
    10.         add index FK36CF3159B3B4FFA (category_id),   
    11.         add constraint FK36CF3159B3B4FFA   
    12.         foreign key (category_id)   
    13.         references CATEGORYS (id)  

    -----------------------------改进代码---------------------------

    1.改进实体类

    Java代码  收藏代码
    1. @Entity  
    2. @Table(name = "tbl_category")  
    3. public class Category {  
    4.   
    5.     @Id  
    6.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
    7.     private Integer id;  
    8.     private String name;  
    9.   
    10.     @ManyToOne  
    11.     @JoinColumn(name = "category_id")  
    12.     private Category parentCategory; // 父类别  
    13.   
    14.     @OneToMany(mappedBy = "parentCategory", targetEntity = Category.class, cascade = CascadeType.ALL)  
    15.     private Set<Category> childCategories = new HashSet<Category>(); // 子类别  
    16.   
    17.     //getter and setter  
    18.   
    19.     /** 添加子类别 */  
    20.     public void addChildCategory(Category category) {  
    21.         if (category == null) {  
    22.             throw new IllegalArgumentException("Can't add a null Category as child.");  
    23.         }  
    24.         // 删除旧的父类别Category  
    25.         if (category.getParentCategory() != null) {  
    26.             category.getParentCategory().getChildCategories().remove(category);  
    27.         }  
    28.         // 设置新的父类别Category  
    29.         category.setParentCategory(this);  
    30.         // 向当前Category对象中加入子类别  
    31.         this.getChildCategories().add(category);  
    32.     }  
    33. }  

    2.测试代码

    Java代码  收藏代码
    1. // 建立食品类别和水果类别之间的关联关系  
    2.             foodCategory.addChildCategory(fruitCategory);  
    3.   
    4.             // 建立食品类别和蔬菜类别之间的关联关系  
    5.             foodCategory.addChildCategory(vegetableCategory);  
    6.   
    7.             // 建立水果类别和苹果类别之间的关联关系  
    8.             fruitCategory.addChildCategory(appleCategory);  
    9.   
    10.             // 建立水果类别和桔子类别之间的关联关系  
    11.             fruitCategory.addChildCategory(orangeCategory);  
    12.   
    13.             // 建立西红柿类别和蔬菜类别之间的关联关系  
    14.             vegetableCategory.addChildCategory(tomatoCategory);  
    15.   
    16.             session.save(foodCategory); 
  • 相关阅读:
    什么样的人适合边打工边创业?
    手机市场分析
    《这个男人来自地球》台词
    关系网成网络盈利模式 LinkedIn网站探秘
    第二届手机应用大赛“金枝奖”评选
    乔布斯的平静让人不寒而栗
    发展移动互联网需理清商业模式
    好想看故乡夏夜的天空
    AdoHelper能否改写成单例模式?
    GMail邀请发放处
  • 原文地址:https://www.cnblogs.com/jason0529/p/3745973.html
Copyright © 2011-2022 走看看