论坛首页 Java企业应用论坛

many to many 的关系如何保存?

浏览 17141 次
该帖已经被评为精华帖
作者 正文
   发表时间:2003-09-16  
配置了many to many关系,
数据读取,建表都没有问题

<bag name="popedoms" lazy="true" table="EXM_ROLE_POPEDOM" cascade="all" inverse="true">
			<key>
				<column name="ROLE_ID" sql-type="INT(10);"/>
			</key>
			<many-to-many class="eg.PopedomVO" insert="true" update="true">
				<column name="POPEDOM_ID" sql-type="INT(10);"/>
			</many-to-many>
		</bag>


但是save的时候,他没有插EXM_ROLE_POPEDOM表,是没有这个功能还是我代码写的不对?

   RoleVO role = (RoleVO); obj;
    if (null != popedoms); {
      List tmp = new ArrayList();;
      for (int i = 0; i < popedoms.length; i++); {
        PopedomVO popdom = dao.selectByPK(popedoms[i].longValue(););;
        //popdom.getRoles();.add(role);;
        tmp.add(popdom);;
      }
      role.setPopedoms(tmp);;
    }
    ds.insert(role);;


其中PopedomVO已经有数据,现新增加一条RoleVO记录,关联上几条PopedomVO数据。但是RoleVO插进去了,关机就是没有,请教了……
   发表时间:2003-09-16  
inverse="true"

这里错了,应该为默认的"false"
0 请登录后投票
   发表时间:2003-09-16  
试过了,也不灵

      <bag name="popedoms" lazy="true" table="EXM_ROLE_POPEDOM" cascade="all" role="popedoms"> 
         <key> 
            <column name="ROLE_ID" sql-type="INT(10);"/> 
         </key> 
         <many-to-many class="cn.mBig.experience.vo.PopedomVO"> 
            <column name="POPEDOM_ID" sql-type="INT(10);"/> 
         </many-to-many> 
      </bag>


    RoleVO role = (RoleVO); obj; 
    PopedomDAO dao = new PopedomDAO(ds);; 
    if (null != popedoms); { 
      List tmp = new ArrayList();; 
      for (int i = 0; i < popedoms.length; i++); { 
        PopedomVO popdom = dao.selectByPK(popedoms[i].longValue(););; 
        tmp.add(popdom);; 
      } 
      role.setPopedoms(tmp);; 
    } 
    session.saveOrUpdate(role);; 
    session.flush();;
0 请登录后投票
   发表时间:2003-09-16  
但是RoleVO插进去了,关机就是没有,请教了……

不是很明白你的意思,请提供详细的代码及映射。
0 请登录后投票
   发表时间:2003-09-16  
打错了,应该是“关系就没保存”
就是Role 和 Popedom 的link table EXM_ROLE_POPEDOM 没有被记录

role 对 popedom是多对多的关系,这两张表通过role_popedom对应起来。

现在有一些popedom记录,希望在插入新的role的时候,同时确定role_popedom的关系并且插到link table中。

role的数据是通过页面输入的,通过checkbox选择需要的popedomId 放入数组。

根据popedomId 取得对应的 popedom实例,增加到role的List形的 popedoms里,保存role.

结果只hibernate只insert了role表,没搭理role_popedom...
0 请登录后投票
   发表时间:2003-09-16  
请提供 完整的 代码及映射。
0 请登录后投票
   发表时间:2003-09-16  
	<class name="eg.RoleVO" table="EXM_ROLE">

		<id name="id" type="java.lang.Long" unsaved-value="0">
			<column name="ID" sql-type="INT(10);"/>
			<generator class="identity"/>
		</id>
		<property name="name">
			<column name="NAME" not-null="true" sql-type="VARCHAR(40);"/>
		</property>
		<property name="creationDate" type="timestamp" column="CREATIONDATE"/>
		<property name="modificationDate" type="timestamp" column="MODIFICATIONDATE"/>

		<property type="java.lang.Long" name="creationUid">
			<column name="CREATION_USER_ID" sql-type="INT(10);"/>
		</property>
		<property type="java.lang.Long" name="modificationUid">
			<column name="MODIFICATION_USER_ID" sql-type="INT(10);"/>
		</property>

		<property name="description">
			<column name="DESCRIPTION" sql-type="VARCHAR(255);"/>
		</property>
		<bag name="popedoms" lazy="true" table="EXM_ROLE_POPEDOM" cascade="all">
			<key>
				<column name="ROLE_ID" sql-type="INT(10);"/>
			</key>
			<many-to-many class="eg.PopedomVO" insert="true" update="true">
				<column name="POPEDOM_ID" sql-type="INT(10);"/>
			</many-to-many>
		</bag>
		<property type="java.lang.Byte" name="state">
			<column name="STATE" sql-type="TINYINT(1);"/>
		</property>
	</class>

	<class name="eg.PopedomVO" table="EXM_POPEDOM">

		<id name="id" type="java.lang.Long" unsaved-value="0">
			<column name="ID" sql-type="INT(10);"/>
			<generator class="identity"/>
		</id>
		<property name="name">
			<column name="NAME" not-null="true" sql-type="VARCHAR(40);"/>
		</property>
		<property name="description">
			<column name="DESCRIPTION" sql-type="VARCHAR(255);"/>
		</property>
	</class>




  private Session s;
  private Long[] popedomPKs;
  
  private void insert(); throws DAOException {
    RoleVO role = (RoleVO); obj; 
    if (null != popedomPKs); { 
      List tmp = new ArrayList();; 
      for (int i = 0; i < popedomPKs.length; i++); { 
        PopedomVO popdom = selectByPK(popedomPKs[i]);; 
        tmp.add(popdom);; 
      } 
      role.setPopedoms(tmp);; 
    } 
    session.saveOrUpdate(role);; 
    session.flush();;
  }

  private PopedomVO selectByPK(Long id); throws
      DAOException {
    PopedomVO vo = null;
    try {
      vo = new PopedomVO();;
      s.load(vo, id);;
    }
    catch (Exception e); {
      throw new DAOException(e);;
    }
    return vo;
  }
0 请登录后投票
   发表时间:2003-09-16  
if (null != popedoms); { 
      List tmp = new ArrayList();; 
      for (int i = 0; i < popedoms.length; i++); { 
        PopedomVO popdom = dao.selectByPK(popedoms[i].longValue(););; 
        tmp.add(popdom);; 
      } 
      role.setPopedoms(tmp);; 
    } 

确定if(null != popedoms)为true吗?
0 请登录后投票
   发表时间:2003-09-16  
确定!贴上的代码把调试输出去掉了,下面的是原来的,
tmp里数据都是正确的

  private Session s;
  private Long[] popedomPKs;
  
  private void insert(); throws DAOException {
    RoleVO role = (RoleVO); obj;
	System.err.println("====================");;
    if (null != popedomPKs); { 
	  System.err.println(popedoms[i]);;
      List tmp = new ArrayList();; 
      for (int i = 0; i < popedomPKs.length; i++); { 
        PopedomVO popdom = selectByPK(popedomPKs[i]);; 
        tmp.add(popdom);; 
      } 
      role.setPopedoms(tmp);; 
    }
	for (java.util.Iterator i = tmp.iterator();; i.hasNext();; ); {
        System.err.println( ( (PopedomVO); i.next(););.getName(););;
    }
    session.saveOrUpdate(role);; 
    session.flush();;
  }
0 请登录后投票
   发表时间:2003-09-16  
把所有代码,映射贴出来吧,帮你调试一下。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics