打个比方,有两个对象,学生,学生证,基于外键一对一的关系,cascade也设置好了,为什么s1.setCertification(c1);没有起作用,学生证表中的stu_id为什么为空呢???非要c1.setStudent(s1)才有用??学生
<hibernate-mapping package="com.yuji.bean.testLazy">
    <class name="Student" table="student" catalog="hibernatelearning">
        <id name="id">
            <column name="Id" />
            <generator class="increment" />
        </id>
        
        <property name="name"/>
        <property name="birth" type="java.util.Calendar"/>
        <property name="describe" column="`describe`"/>
        
        <one-to-one name="certification" property-ref="student" cascade="all"class="StudentCertification"></one-to-one>
 
    </class>
</hibernate-mapping>学生证
<hibernate-mapping package="com.yuji.bean.testLazy">
    <class name="StudentCertification" table="student_certification" catalog="hibernatelearning">
        <id name="id">
            <column name="Id" />
            <generator class="increment" />
        </id>
        
        <property name="sellCardId" column="sell_card_id"/>
        <property name="describe" column="`describe`"/>
        
        <many-to-one name="student" class="Student" column="stu_id" unique="true"></many-to-one>    
    </class>
</hibernate-mapping>主要代码:
Student s1 = new Student();
s1.setName("s1");
s1.setBirth(Calendar.getInstance());
s1.setDescribe("s1_des");

Student s2 = new Student();
s2.setName("s2");
s2.setBirth(Calendar.getInstance());
s2.setDescribe("s2_des");

Student s3 = new Student();
s3.setName("s3");
s3.setBirth(Calendar.getInstance());
s3.setDescribe("s3_des");

StudentCertification  c1 = new StudentCertification();
c1.setSellCardId("sell_1");
c1.setDescribe("c1_des");

StudentCertification  c2 = new StudentCertification();
c2.setSellCardId("sell_2");
c2.setDescribe("c2_des");

StudentCertification  c3 = new StudentCertification();
c3.setSellCardId("sell_3");
c3.setDescribe("c3_des");

Session session1 =  HibernateSessionFactory.getSession();
Transaction tran = session1.beginTransaction();

s1.setCertification(c1);
s2.setCertification(c2);
s3.setCertification(c3);

session1.save(s1);
session1.save(s2);
session1.save(s3);
tran.commit();
HibernateSessionFactory.closeSession();

解决方案 »

  1.   

    很明显,应该是被延迟加载了,改成这样试一下
    <one-to-one name="certification" property-ref="student" cascade="all"class="StudentCertification" lazy="false"></one-to-one>
      

  2.   

    <one-to-one name="certification" property-ref="student" cascade="all"class="StudentCertification" lazy="false"></one-to-one>
      

  3.   

    如果Hibernate没有使用得当,有时候映射出来的实体关系是会违背数据库设计的理论基础的。
    而且有时候因为没有配置好,老是会出现两个实体之前出现”鸡生蛋,蛋生鸡“的现象!基于你的例子,首先应该遵守数据库设计理论设计出2个实体。
    1).学生(PK_stuID,其他字段...)
    2).学生证(FK_stuID,其他字段...)学生表是主键表,学生证表是外键表。在业务逻辑中,一般都是学生做为主动方。
    所以,映射关系如下(只给出关联字段部分):
    学生表中有一个证书字段[private StudentCertification certification;],
    关联部分映射内容如下:
    <id name="deviceID" type="string" length="12">
        <generator class="assigned"></generator>
    </id><!--主动方级联所有操作-->
    <one-to-one name="certification" class="StudentCertification" cascade="all"></one-to-one>学生证表中有一个学生字段(private Student student;),
    关联部分映射内容如下:<id name="deviceID">
        <generator class="foreign">
            <param name="property">student</param>
        </generator>
    </id><!--被动方不需要级联-->
    <one-to-one 
          name="student"
          class="Student" 
          cascade="none" 
          foreign-key="FK_StuToCert_1vs1"
          outer-join="auto" 
          constrained="true"
      />
      

  4.   


    我的意思是为什么s1.setCertification(c1);这句语句没有维护外键。。从对象模型上来说这是正确的啊,为什么hibernate不能识别
      

  5.   

    StudentCertification c1 = new StudentCertification(); 
    是表对象撒  
    你看下你的程序 这个 C1 对象的数据都没保存到StudentCertification这张表里面 
    然后你就
    s1.setCertification(c1);
    session1.save(s1);
    也就是c1就不存在  你怎么保存的了
      

  6.   


    可是我已经配置了cascade为ALL了啊!!!
      

  7.   

    <many-to-one name="student" class="Student" column="stu_id" unique="true"></many-to-one>   
      </class>
    一对一的关系 为什么用 many-to-one ????????????
      

  8.   


    你是基于主键的一对一关联,基于你的例子,首先应该遵守数据库设计理论设计出2个实体。
    1).学生(PK_stuID,其他字段...)
    2).学生证(FK_stuID,其他字段...)我的意思是学生证表中有自己的主键!!我们从一开始就打岔了。。
      

  9.   

    我也遇到同样的问题,我估计可能是<one-to-one>这个标签  不会自动的去维护两者之间的关系,这个<one-to-many>和<many-to-one>是不一样的
      

  10.   

    怎么会学生表one-to-one  另外一张表用的many-to-one。