Hibernate自关联关系

自关联的例子:我觉得自关联好像就是当表存在自包含关系就是自己还可以再细分的情况下应用的!一直不知道怎么理解好,暂时就这么理解吧!

业务逻辑:<o:p></o:p>

书籍的种类,本身是自关联的关系,如下图所示:<o:p></o:p>

所有书籍:<o:p></o:p>

历史书籍<o:p></o:p>

音乐书籍<o:p></o:p>

           钢琴书籍<o:p></o:p>

烹饪书籍<o:p></o:p>

           美食书籍

1.     Books类的源程序 代码的目录结构 Classes                   Hibernate.property        /mypack                   Books.java                   BusinessService.java                   Books.hbm.xml book.java

 
  1. package mypack;  
  2. import java.util.Set;  
  3. import java.io.Serializable;   
  4. public class Books  
  5.     implements Serializable {  
  6. /**  
  7.    * 默认构造函数
  8.    */ 
  9. public Books() {  
  10. }   
  11. /** 主健id */ 
  12. private Long id;  
  13.    
  14. /** 书籍名称 */ 
  15. private String name;  
  16. /** 父书籍 */ 
  17. private mypack.Books parentCategory;   
  18. /** 子集合 */ 
  19. private Set childCategories;   
  20. /** 完整构造函数 */ 
  21. public Books(String name, mypack.Books parentCategory, Set childCategories) {  
  22.     this.name = name;  
  23.     this.parentCategory  
  24. = parentCategory;   
  25.     this.childCategories = childCategories;  
  26. }  
  27. /** 最小构造函数 */ 
  28. public Books(Set childCategories) {  
  29.     this.childCategories = childCategories;  
  30. }   
  31. public Long getId() {  
  32.     return this.id;  
  33. }  
  34. public void setId(Long id) {  
  35.     this.id = id;  
  36. }  
  37.    
  38. public String getName() {  
  39.     return this.name;  
  40. }  
  41.    
  42. public void setName(String name) {  
  43.     this.name = name;  
  44. }  
  45.    
  46. public mypack.Books getParentCategory() {  
  47.     return this.parentCategory;  
  48. }  
  49.    
  50. public void setParentCategory(mypack.Books parentCategory) {  
  51.     this.parentCategory = parentCategory;  
  52. }  
  53.    
  54. public Set getChildCategories() {  
  55.     return this.childCategories;  
  56. }  
  57.    
  58. public void setChildCategories(Set childCategories) {  
  59.     this.childCategories = childCategories;  
  60. }  
  61. }  
  62.    

  映射文件,放在classes/mypack<o:p></o:p>

                        Books.hbm.xml

xml 代码
 
  1. <!---->xml versio  
  2. n="1.0"?>   
  3. <!---->
  4. PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"  
  5. "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> 
  6. <hibernate-mapping > 
  7.    
  8. <class name="mypack.Books" table="books" > 
  9.     <id name="id" type="long" column="ID"> 
  10.       <generator class="increment"/> 
  11.     id> 
  12.    
  13.     <property name="name" type="string" > 
  14.         <column name="NAME" length="15" /> 
  15.     property> 
  16.    
  17.     <set   
  18.         name="childCategories" 
  19.         cascade="save-update" 
  20.         inverse="true" 
  21.         > 
  22.         <key column="CATEGORY_ID" /> 
  23.         <one-to-many class="mypack.Books" /> 
  24.      set>     
  25.    
  26.    <many-to-one 
  27.         name="parentCategory" 
  28.         column="CATEGORY_ID" 
  29.         class="mypack.Books" 
  30. cascade="save-update"   
  31.        /> 
  32.    
  33. class> 
  34.    
  35. hibernate-mapping> 

数据库Schema<o:p></o:p>

数据库Schema <o:p></o:p>

xml 代码
  1. Create table books(  
  2.        Id number(10) not null,  
  3.        Name varchar2(64),  
  4. Category_id number(10),  
  5. Primary key(id)  
  6. );  
  7.    
  8.    
  9. 1.       hibernate.property  
  10. hibernate.property   
  11. hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect  
  12. hibernate.dialect net.sf.hibernate.dialect.OracleDialect  
  13. hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver  
  14. hibernate.connection.username testpm  
  15. hibernate.connection.password testpm  
  16. hibernate.connection.url jdbc:oracle:thin:@localhost:1521:wsy  

 

posted @ 2013-08-14 15:10  hanks  阅读(244)  评论(0)    收藏  举报