zoukankan      html  css  js  c++  java
  • 精通Hibernate类与类关联关系:[三]映射一对多双向自身关联关系

    三、映射一对多双向自身关联关

    Category.java:

    1. package mypack; 
    2. import java.util.HashSet; 
    3. import java.util.Set; 
    4. publicclass Category  implements java.io.Serializable { 
    5.      privatelong id; 
    6.      private String name; 
    7.      private Set childCategories = new HashSet(0); 
    8.      private Category parentCategory; 
    9.     public Category() { 
    10.                 } 
    11.     public Category(String name, Set childCategories, Category parentCategory) { 
    12.        this.name = name; 
    13.        this.childCategories = childCategories; 
    14.        this.parentCategory = parentCategory; 
    15.     } 
    16.     
    17.     publiclong getId() { 
    18.         returnthis.id; 
    19.     } 
    20.      
    21.     publicvoid setId(long id) { 
    22.         this.id = id; 
    23.     } 
    24.     public String getName() { 
    25.         returnthis.name; 
    26.     } 
    27.      
    28.     publicvoid setName(String name) { 
    29.         this.name = name; 
    30.     } 
    31.     public Set getChildCategories() { 
    32.         returnthis.childCategories; 
    33.     } 
    34.      
    35.     publicvoid setChildCategories(Set childCategories) { 
    36.         this.childCategories = childCategories; 
    37.     } 
    38.     public Category getParentCategory() { 
    39.         returnthis.parentCategory; 
    40.     } 
    41.      
    42.     publicvoid setParentCategory(Category parentCategory) { 
    43.         this.parentCategory = parentCategory; 
    44.     } 
    45. }

          配置文件Category.hbm.xml:

    1. <?xml version="1.0"?> 
    2. <!DOCTYPE hibernate-mapping 
    3. PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
    5. <hibernate-mapping > 
    6.   <class name="mypack.Category" table="CATEGORIES"
    7.     <id name="id" type="long" column="ID"
    8.       <generator class="increment"/> 
    9.     </id> 
    10.     <property name="name" type="string"
    11.         <column name="NAME" length="15" /> 
    12.     </property> 
    13.     <set  
    14.         name="childCategories"
    15.         cascade="save-update"
    16.         inverse="true"
    17.         > 
    18.         <key column="CATEGORY_ID" /> 
    19.         <one-to-many class="mypack.Category" /> 
    20.      </set>    
    21.    <many-to-one 
    22.         name="parentCategory"
    23.         column="CATEGORY_ID"
    24.         class="mypack.Category"
    25.        /> 
    26.   </class
    27. </hibernate-mapping>
  • 相关阅读:
    Spring的事务 之 9.4 声明式事务 ——跟我学spring3
    我对AOP的理解
    基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
    我对IoC/DI的理解
    Spring对事务管理的支持的发展历程(基础篇)
    Tomcat一个BUG造成CLOSE_WAIT
    用dubbo时遇到的一个序列化的坑
    只写完功能代码仅仅只是开始
    事物隔离级别和乐观锁
    关于ubuntu实机与虚机互相copy
  • 原文地址:https://www.cnblogs.com/wukong0214/p/2872905.html
Copyright © 2011-2022 走看看