Java Study Center  
          日歷
          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789
          統計
          • 隨筆 - 40
          • 文章 - 3
          • 評論 - 0
          • 引用 - 0

          導航

          常用鏈接

          留言簿(1)

          隨筆檔案(40)

          文章檔案(3)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           
           以many-to-many為例,有Position和User兩張表,一個Position可以有多個Users,一個User也可以有多個Position,中間的關聯表為 test_user_position 。通過在PO中加入XDoclet,自動生成hbm配置文件。不廢話,看代碼。

          Position.java
          package test;

          import java.util.Set;
          import java.util.TreeSet;

          /**
           * @hibernate.class table="test_position"
           
          */

          public class Position {

              
          private int id;

              
          private int name;

              
          private Set<Users> users = new TreeSet<Users>();

              
          /**
               * @hibernate.id generator-class="identity" type="int"
               
          */

              
          public int getId() {
                  
          return id;
              }


              
          public void setId(int id) {
                  
          this.id = id;
              }


              
          /**
               * @hibernate.property length="25"
               
          */

              
          public int getName() {
                  
          return name;
              }


              
          public void setName(int name) {
                  
          this.name = name;
              }


              
          /**
                * @hibernate.set inverse="true" lazy="true" table="test_user_position"
                * @hibernate.collection-key column="position_id"
                * @hibernate.collection-many-to-many class="test.Users" column="user_id"
                
          */

              
          public Set<Users> getUsers() {
                  
          return users;
              }


              
          public void setUsers(Set<Users> users) {
                  
          this.users = users;
              }
              
          }


          接下來是Users.java
          package test;

          import java.util.*;

          /**
           * @hibernate.class table="test_uses"
           
          */

          public class Users {

              
          private int id;

              
          private String name;

              
          private Set<Position> positions = new TreeSet<Position>();

              
          /**
               * @hibernate.id generator-class="identity" typ="int"
               
          */

              
          public int getId() {
                  
          return id;
              }


              
          public void setId(int id) {
                  
          this.id = id;
              }


              
          /**
               * @hibernate.property length="25"
               
          */

              
          public String getName() {
                  
          return name;
              }


              
          public void setName(String name) {
                  
          this.name = name;
              }


              
          /**
                * @hibernate.set table="test_user_position" lazy="true"
                * @hibernate.collection-key column="user_id"
                * @hibernate.collection-many-to-many class="test.Position" column="position_id"
                
          */

              
          public Set<Position> getPositions() {
                  
          return positions;
              }


              
          public void setPositions(Set<Position> positions) {
                  
          this.positions = positions;
              }
              
          }


               在Myeclipse中右鍵點擊項目,選擇“Properties",從界面中選擇”Myeclipse/XDoclet",點擊“Add Standard...”,添加“Standard Hibernate”,點擊OK,結束設置。

               在項目中建立Hibernate.cfg.xml,配置好SessionFactory和數據源

               右鍵點擊項目,選擇“Myeclipse/Run XDoclet",將自動創建以上兩個類對應的hbm文件。

               注意:創建完成的hbm文件存在問題,里面有role和readonly屬性,將前者改為name,后者刪掉即可。
          posted on 2008-07-31 11:28 綠茶_鄭州 閱讀(265) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          Copyright © 綠茶_鄭州 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 遂溪县| 屯留县| 会宁县| 永靖县| 吉木乃县| 巴林右旗| 莫力| 南华县| 历史| 新安县| 米易县| 多伦县| 奉贤区| 小金县| 渭南市| 通州区| 黄山市| 三明市| 定远县| 响水县| 武义县| 凌源市| 韶山市| 东至县| 广灵县| 秦皇岛市| 留坝县| 盐亭县| 来凤县| 杭锦旗| 静宁县| 舞阳县| 连南| 万载县| 准格尔旗| 临武县| 贞丰县| 望都县| 浪卡子县| 常德市| 定西市|