zhyiwww
          用平實的筆,記錄編程路上的點點滴滴………
          posts - 536,comments - 394,trackbacks - 0

          這幾天,在多隊多的中間表的操作上遇到了點麻煩,總是不能刪除和更新中間表,因為,我使用的是一隊多的關聯,
          映射的是中間表,所以,我雖然操作了set,但是總是不能反映到中間表上去。

          后來使用如下的方法來解決,看如下的例子:

          (1)三個表

          CREATE TABLE `poi` (
          ? `poi_id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
          ? `poi_name` VARCHAR(45) COLLATE latin1_swedish_ci NOT NULL DEFAULT '',
          ? PRIMARY KEY (`poi_id`),
          ? UNIQUE KEY `poi_name` (`poi_name`)

          )ENGINE=InnoDB
          AUTO_INCREMENT=15 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
          COMMENT='InnoDB free: 3072 kB';


          CREATE TABLE `user` (
          ? `user_id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
          ? `username` VARCHAR(45) COLLATE latin1_swedish_ci NOT NULL DEFAULT '',
          ? `password` VARCHAR(45) COLLATE latin1_swedish_ci NOT NULL DEFAULT '',
          ? PRIMARY KEY (`user_id`),
          ? UNIQUE KEY `username` (`username`)

          )ENGINE=InnoDB
          AUTO_INCREMENT=16 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
          COMMENT='InnoDB free: 3072 kB';


          關聯表

          CREATE TABLE `user_poi` (
          ? `user_id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
          ? `poi_id` INTEGER(10) UNSIGNED NOT NULL,
          ? KEY `FK_user_pois_poiid` (`poi_id`),
          ? KEY `FK_user_pois_userid` (`user_id`),
          ? CONSTRAINT `FK_user_pois_poiid` FOREIGN KEY (`poi_id`) REFERENCES `poi` (`poi_id`) ON DELETE CASCADE ON UPDATE CASCADE,
          ? CONSTRAINT `FK_user_pois_userid` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE

          )ENGINE=InnoDB
          AUTO_INCREMENT=3 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
          COMMENT='InnoDB free: 3072 kB; (`poi_id`) REFER `demo/poi`(`poi_id`) ON UPDATE CASCADE; (';


          (2)POJO


          /**
          ?* @hibernate.class table="user"
          ?*/
          public class User implements java.io.Serializable {

          ??? // Fields

          ??? private Integer userid;
          ??? private String username;
          ??? private String password;

          ??? /**
          ??? ?* Poi set
          ??? ?*/
          ??? private Set<Poi> pois = new HashSet();
          ???
          ???
          ???
          ??? // Constructors

          ??? /**
          ??? ?* @hibernate.set table="user_poi" cascade="all"
          ??? ?* @hibernate.collection-key column="user_id"
          ??? ?* @hibernate.collection-many-to-many column="poi_id" class="org.zy.pro.pd.dao.Poi"
          ??? ?*/
          ??? public Set getPois() {
          ??? ??? return pois;
          ??? }

          ??? public void setPois(Set pois) {
          ??? ??? this.pois = pois;
          ??? }

          ??? public User() {
          ??? }

          ??? /** full constructor */
          ??? public User(String username, String password) {
          ??? ??? this.username = username;
          ??? ??? this.password = password;
          ??? }

          ??? // Property accessors


          ??? /**
          ??? ?* @hibernate.id column="user_id" generator-class="increment"
          ??? ?*/
          ??? public Integer getUserid() {
          ??? ??? return this.userid;
          ??? }

          ??? public void setUserid(Integer userid) {
          ??? ??? this.userid = userid;
          ??? }

          ??? /**
          ??? ?* @hibernate.property column="username"
          ??? ?* @return
          ??? ?*/
          ??? public String getUsername() {
          ??? ??? return this.username;
          ??? }

          ??? public void setUsername(String username) {
          ??? ??? this.username = username;
          ??? }

          ??? /**
          ??? ?* @hibernate.property column="password"
          ??? ?* @return
          ??? ?*/
          ??? public String getPassword() {
          ??? ??? return this.password;
          ??? }

          ??? public void setPassword(String password) {
          ??? ??? this.password = password;
          ??? }

          }


          /**
          ?* @hibernate.class table="poi"
          ?*/
          public class Poi? implements java.io.Serializable {


          ??? // Fields???

          ???? private Integer poiId;
          ???? private String poiName;


          ??? // Constructors

          ??? /** default constructor */
          ??? public Poi() {
          ??? }

          ??? /** minimal constructor */
          ??? public Poi(String poiName) {
          ??????? this.poiName = poiName;
          ??? }


          ??? /**
          ???? * @hibernate.id column="poi_id" generator-class="increment"
          ???? */
          ??? public Integer getPoiId() {
          ??????? return this.poiId;
          ??? }
          ???
          ??? public void setPoiId(Integer poiId) {
          ??????? this.poiId = poiId;
          ??? }

          ??? /**
          ???? * @hibernate.property column="poi_name"
          ???? * @return
          ???? */
          ??? public String getPoiName() {
          ??????? return this.poiName;
          ??? }
          ???
          ??? public void setPoiName(String poiName) {
          ??????? this.poiName = poiName;
          ??? }
          }



          (3)hibernate mapping

          利用xDolect根據bean生成映射

          Poi.hbm.xml

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE hibernate-mapping PUBLIC
          ??? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          ??? "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

          <hibernate-mapping
          >
          ??? <class
          ??????? name="org.zy.pro.pd.dao.Poi"
          ??????? table="poi"
          ??? >

          ??????? <id
          ??????????? name="poiId"
          ??????????? column="poi_id"
          ??????????? type="java.lang.Integer"
          ??????? >
          ??????????? <generator class="increment">
          ????
          ??????????? </generator>
          ??????? </id>

          ??????? <property
          ??????????? name="poiName"
          ??????????? type="java.lang.String"
          ??????????? update="true"
          ??????????? insert="true"
          ??????????? column="poi_name"
          ??????? />

          ??? </class>

          </hibernate-mapping>


          User.hbm.xml

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE hibernate-mapping PUBLIC
          ??? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          ??? "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

          <hibernate-mapping
          >
          ??? <class
          ??????? name="org.zy.pro.pd.dao.User"
          ??????? table="user"
          ??? >

          ??????? <id
          ??????????? name="userid"
          ??????????? column="user_id"
          ??????????? type="java.lang.Integer"
          ??????? >
          ??????????? <generator class="increment">

          ??????????? </generator>
          ??????? </id>

          ??????? <set
          ??????????? name="pois"
          ??????????? table="user_poi"
          ??????????? lazy="false"
          ??????????? cascade="all"
          ??????????? sort="unsorted"
          ??????? >

          ??????????? <key
          ??????????????? column="user_id"
          ??????????? >
          ??????????? </key>

          ??????????? <many-to-many
          ??????????????? class="org.zy.pro.pd.dao.Poi"
          ??????????????? column="poi_id"
          ??????????????? outer-join="auto"
          ???????????? />

          ??????? </set>

          ??????? <property
          ??????????? name="username"
          ??????????? type="java.lang.String"
          ??????????? update="true"
          ??????????? insert="true"
          ??????????? column="username"
          ??????? />

          ??????? <property
          ??????????? name="password"
          ??????????? type="java.lang.String"
          ??????????? update="true"
          ??????????? insert="true"
          ??????????? column="password"
          ??????? />



          ??? </class>

          </hibernate-mapping>


          (4)關聯添加
          ??????? UserDAO ud = new UserDAO();
          ??? ??? List l = ud.findAll();
          ??? ??? assertNotNull(l);
          ??? ???
          ??? ??? User u = ud.findById(2);
          ??? ??? assertNotNull(u);
          ??? ???
          ??? ??? Set pois = u.getPois();
          ??? ???
          ??? ??? Poi p = new Poi("Moc"+new java.util.Random().nextInt(1000000));
          ??? ??? System.out.print(p.getPoiId());
          ??? ???
          ??? ???
          ??? ??? pois.add(p);

          //關聯刪除
          //??? ??? u.setPois(null);
          ??? ???
          ??? ??? Poi pp = pd.findById(12);
          ??? ??? pois.remove(pp);
          ??? ???
          ??? ???
          ??? ??? ud.begin();
          ??? ??? ud.merge(u);
          ??? ??? ud.commit();


          // 關聯更新也可以,只要改變set就可以了。



          使用總結
          [1]使用多對多關系
          ??? 在映射的是時候要使用many-to-many
          ??? 所以,在user的set集合里面直接存放的是Poi的對象,而不是user_poi表的對象
          [2]reverse : false
          ??? 允許反轉操作
          [3]casecade : all
          ??? 只要允許級聯操作才可以實現級聯的刪除和更新

          如果使用一對多,就只能更新中間表的普通字段,不能更新主鍵字段,也不能刪除中間表記錄
          所以只能維護中間表的關聯,但是不能更新中間表的信息。





          |----------------------------------------------------------------------------------------|
                                     版權聲明  版權所有 @zhyiwww
                      引用請注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2009-02-16 13:14 zhyiwww 閱讀(10999) 評論(7)  編輯  收藏 所屬分類: java basicj2ee

          FeedBack:
          # re: hibernate多對多刪除和更新中間表
          2009-02-27 13:04 | 張梁
          根本沒什么東西  回復  更多評論
            
          # re: hibernate多對多刪除和更新中間表
          2009-03-22 01:15 | 多幅
          還寫得亂七八招。  回復  更多評論
            
          # re: hibernate多對多刪除和更新中間表[未登錄]
          2009-03-25 06:35 | aaaa
          關鍵的代碼沒有  回復  更多評論
            
          # re: hibernate多對多刪除和更新中間表
          2009-04-23 15:23 | LD
          沒實質內容!  回復  更多評論
            
          # re: hibernate多對多刪除和更新中間表
          2009-05-22 15:15 | 其二千萬
          寫了些什么玩意啊,亂七八糟的!~  回復  更多評論
            
          # re: hibernate多對多刪除和更新中間表
          2009-09-24 13:51 | --
          [3]casecade : all
          這樣設置的話,如果刪除一方,也會刪除另一方的吧,你不會這么狠吧?
          刪除一方時,只要關聯的刪除關系就行了!
          所以設置為save-update  回復  更多評論
            
          # re: hibernate多對多刪除和更新中間表
          2009-10-13 15:49 | zhyiwww
          關于casecade設置為all,是測試使用,如果是開發,可以根據需要來自己設定。
            回復  更多評論
            
          主站蜘蛛池模板: 泰州市| 淳化县| 惠安县| 盖州市| 饶河县| 隆回县| 大连市| 莎车县| 榆林市| 平阴县| 广灵县| 福贡县| 泗洪县| 华宁县| 沾益县| 瓦房店市| 鄂托克前旗| 五指山市| 武清区| 确山县| 福海县| 依安县| 孟津县| 临颍县| 崇州市| 北流市| 祁连县| 崇礼县| 唐海县| 台东市| 东乌| 永胜县| 昭通市| 登封市| 东乌珠穆沁旗| 太仓市| 黄大仙区| 洪湖市| 通化县| 澄城县| 桓台县|