new的Object是一个VO,不是被Hibernate管理的PO,而load来的就是;
1:
Parent p = new Parent ();
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(p);
session.flush();
照你这么写的,根本就只会插入parent的数据而已.child是否从session中load的?

解决方案 »

  1.   

    有两个问题:
    一、session.save(p),这里应该是save many的对象还是one的对象?
    如果是save one的对象,那么hibernate的那份reference有错?
    如果是save many的对象,如果Parent的Child一下子增加多个,该怎么Save??书上的两种做法让我糊涂了!!二、你说的不是PO,但是经过session.save(p)处理它不是由VO变成PO了么?问题多多~~~~~~~~~~
      

  2.   

    贴出代码:
    many的xml配置文件:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
        
    <hibernate-mapping>
    <!-- 
        Created by the Middlegen Hibernate plugin 2.1    http://boss.bekk.no/boss/middlegen/
        http://www.hibernate.org/
    --><class 
        name="com.digipower.demo.vo.PCgCodeGVO" 
        table="P_CGCODE_G"
        dynamic-update="true"
        dynamic-insert="true"
    >
        <meta attribute="class-description" inherit="false">
           @hibernate.class
            table="P_CGCODE_G" 
            dynamic-update="true"
            dynamic-insert="true"
        </meta>    <id
            name="pcgcodeGId"
            type="int"
            column="P_CGCODE_G_ID"
            unsaved-value="0"
        >
            <meta attribute="field-description">
               @hibernate.id
                generator-class="assigned"
                type="int"
                column="P_CGCODE_G_ID"
            </meta>
            <generator class="assigned" />
        </id>    <property
            name="adClientId"
            type="int"
            column="AD_CLIENT_ID"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="AD_CLIENT_ID"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="adOrgId"
            type="int"
            column="AD_ORG_ID"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="AD_ORG_ID"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="isactive"
            type="java.lang.String"
            column="ISACTIVE"
            not-null="true"
            length="1"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="ISACTIVE"
                length="1"
                not-null="true"
            </meta>    
        </property>
        <property
            name="created"
            type="java.sql.Timestamp"
            column="CREATED"
            not-null="true"
            length="7"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="CREATED"
                length="7"
                not-null="true"
            </meta>    
        </property>
        <property
            name="createdby"
            type="int"
            column="CREATEDBY"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="CREATEDBY"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="updated"
            type="java.sql.Timestamp"
            column="UPDATED"
            not-null="true"
            length="7"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="UPDATED"
                length="7"
                not-null="true"
            </meta>    
        </property>
        <property
            name="updatedby"
            type="int"
            column="UPDATEDBY"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="UPDATEDBY"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="cgvalue"
            type="java.lang.String"
            column="CGVALUE"
            not-null="true"
            length="80"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="CGVALUE"
                length="80"
                not-null="true"
            </meta>    
        </property>
        <property
            name="name"
            type="java.lang.String"
            column="NAME"
            not-null="true"
            length="120"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="NAME"
                length="120"
                not-null="true"
            </meta>    
        </property>
        <property
            name="location"
            type="java.lang.String"
            column="LOCATION"
            not-null="true"
            length="20"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="LOCATION"
                length="20"
                not-null="true"
            </meta>    
        </property>
        <property
            name="description"
            type="java.lang.String"
            column="DESCRIPTION"
            length="510"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="DESCRIPTION"
                length="510"
            </meta>    
        </property>
        <property
            name="help"
            type="java.lang.String"
            column="HELP"
            length="4000"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="HELP"
                length="4000"
            </meta>    
        </property>    <!-- Associations -->
      
        <!-- bi-directional one-to-many association to PcgcodeK -->
        <set
            name="pcgcodeKs"
            lazy="false"
            inverse="false"
            cascade="all"
        >
            <meta attribute="field-description">
               @hibernate.set
                lazy="false"
                inverse="false"
                cascade="all"           @hibernate.collection-key
                column="P_CGCODE_G_ID"           @hibernate.collection-one-to-many
                class="com.digipower.demo.vo.PCgCodeKVO"
            </meta>
            <key>
                <column name="P_CGCODE_G_ID" />
            </key>
            <one-to-many 
                class="com.digipower.demo.vo.PCgCodeKVO"
            />
        </set></class>
    </hibernate-mapping>
      

  3.   

    One的xml:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
        
    <hibernate-mapping>
    <!-- 
        Created by the Middlegen Hibernate plugin 2.1    http://boss.bekk.no/boss/middlegen/
        http://www.hibernate.org/
    --><class 
        name="com.digipower.demo.vo.PCgCodeKVO" 
        table="P_CGCODE_K"
        dynamic-update="false"
        dynamic-insert="false"
        batch-size="100"
    >
        <meta attribute="class-description" inherit="false">
           @hibernate.class
            table="P_CGCODE_K"
            dynamic-update="false"
         dynamic-insert="false"
                    batch-size="100"
        </meta>    <id
            name="pcgcodeKId"
            type="int"
            column="P_CGCODE_K_ID"
            unsaved-value="0"
        >
            <meta attribute="field-description">
               @hibernate.id
                generator-class="assigned"
                type="int"
                column="P_CGCODE_K_ID"
            </meta>
            <generator class="assigned" />
        </id>    <property
            name="adClientId"
            type="int"
            column="AD_CLIENT_ID"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="AD_CLIENT_ID"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="adOrgId"
            type="int"
            column="AD_ORG_ID"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="AD_ORG_ID"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="isactive"
            type="java.lang.String"
            column="ISACTIVE"
            not-null="true"
            length="1"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="ISACTIVE"
                length="1"
                not-null="true"
            </meta>    
        </property>
        <property
            name="created"
            type="java.sql.Timestamp"
            column="CREATED"
            not-null="true"
            length="7"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="CREATED"
                length="7"
                not-null="true"
            </meta>    
        </property>
        <property
            name="createdby"
            type="int"
            column="CREATEDBY"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="CREATEDBY"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="updated"
            type="java.sql.Timestamp"
            column="UPDATED"
            not-null="true"
            length="7"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="UPDATED"
                length="7"
                not-null="true"
            </meta>    
        </property>
        <property
            name="updatedby"
            type="int"
            column="UPDATEDBY"
            not-null="true"
            length="10"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="UPDATEDBY"
                length="10"
                not-null="true"
            </meta>    
        </property>
        <property
            name="cgvalue"
            type="java.lang.String"
            column="CGVALUE"
            not-null="true"
            length="80"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="CGVALUE"
                length="80"
                not-null="true"
            </meta>    
        </property>
        <property
            name="name"
            type="java.lang.String"
            column="NAME"
            not-null="true"
            length="120"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="NAME"
                length="120"
                not-null="true"
            </meta>    
        </property>
        <property
            name="location"
            type="java.lang.String"
            column="LOCATION"
            not-null="true"
            length="20"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="LOCATION"
                length="20"
                not-null="true"
            </meta>    
        </property>
        <property
            name="description"
            type="java.lang.String"
            column="DESCRIPTION"
            length="510"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="DESCRIPTION"
                length="510"
            </meta>    
        </property>
        <property
            name="help"
            type="java.lang.String"
            column="HELP"
            length="4000"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="HELP"
                length="4000"
            </meta>    
        </property>    <!-- Associations -->
      
        <!-- bi-directional many-to-one association to PcgcodeG -->
        <many-to-one
            name="pCgCodeGVO"
            class="com.digipower.demo.vo.PCgCodeGVO"
            not-null="true"
      cascade="none"
    outer-join="auto"
    update="true"
    insert="true"
    access="property"
            column="P_CGCODE_G_ID"
        >
            <meta attribute="field-description">
               @hibernate.many-to-one
                not-null="true"
                update="true"
                insert="true"
                 cascade="none"
    outer-join="auto"
    update="true"
    insert="true"
    access="property"
               @hibernate.column name="P_CGCODE_G_ID"         
            </meta>
            <column name="P_CGCODE_G_ID" />
        </many-to-one></class>
    </hibernate-mapping>
    存储代码:
      PCgCodeGVO gvo = new PCgCodeGVO();  /** 设置VO对象的主键值 */
      gvo.setPcgcodeGId(Integer.valueOf("1000"7));  PCgCodeKVO kvo = new PCgCodeKVO();
      
      kvo.setPcgcodeKId(Integer.valueOf("111"));  kvo.setpCgCodeGVO(gvo);
      gvo.getPcgcodeKs().add(kvo);  session.save(gvo);    //这里写成save(kvo)也不成两天了,郁闷死我了
      

  4.   

    汗,
    Parent p = new Parent ();
    p.setId()
    Child c = new Child();
    c.setParent(p);
    p.getChildren().add(c);
    session.save(p);
    session.flush();
    像这样写是正确的,会产生两条Insert语句,需要设置one这端cascade=all.
    呵呵,没这样用过.
      

  5.   

    奇怪啊,不论是在one这端设置inverse="true",还是"false"都可以的,唯一区别就是one这端设置inverse="false"时会多一条更新many这端对象引用one主键的update的sql.以下测试过了,没问题.
    Parent p = new Parent ();
    p.setId("...")
    Child c1 = new Child();
    c1.setId("...");
    c1.setParent(p);
    Child c2 = new Child();
    c2.setId("...");
    c2.setParent(p);
    p.getChildren().add(c1);
    p.getChildren().add(c2);
    session.save(p);
    session.flush();
      

  6.   

    为什么我再用 save(gvo)的时候总是会是这样的呢??Hibernate: insert into P_CGCODE_G (AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, CGVALUE, NAME, LOCATION, DESCRIPTION, HELP, P_CGCODE_G_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)Hibernate: update P_CGCODE_K set AD_CLIENT_ID=?, AD_ORG_ID=?, ISACTIVE=?, CREATED=?, CREATEDBY=?, UPDATED=?, UPDATEDBY=?, CGVALUE=?, NAME=?, LOCATION=?, DESCRIPTION=?, HELP=?, P_CGCODE_G_ID=? where P_CGCODE_K_ID=?GAction afterSave test对从表它总是只有一个update的sql,高手救救咱啊
      

  7.   

    嗯,应该是这样的,我使用的uuid.hex(但还是有点奇怪,我本来期待当parent主控时失败的.....)
    而你的key策略是assigned,由parent主控,cascade=all,这样当你sessison.save(parent)时,hibernate发现其子的id不为null,于是认为这是一个PO,于是调用update语句更新所有child的状态.
    事实上,应该让child主控的<set inverse="true" ....
    然后:
    //从其它层得到parent
    session.save(parent);
    //从其它层得到children
    List children=....;
    for (Iterator it = children.iterator(); it.hasNext();) {
      Child child=(Child)it.next();
      //这里的parent已经是一个po了,但事实上使用一个VO也成,cascade=none时,hibernate不会关心parent的状态的
      child.setParent(parent);
      session.save(child);
    }
    //这样作,应该是没有任何问题的,我一直都是这样用的.但你采用assigned时还是要小心一点,千万不要使用saveOrUpdate()
      

  8.   

    恩,谢谢楼上的老兄我得好好把hibernate reference  参考一下不知道楼上的兄台是怎么学hibernate的什么好的资料?
      

  9.   

    就是hibernate reference了,多写程序,碰到问题马上去查reference.