Foreign key (FK8FFE823B8197F362:student [subjectid])) must have same number of columns as the referenced primary key (subject [id,subjectName]),这是错误提示,请问该怎么解决?
student配置文件如下:
 <class name="com.yourcompany.LoginForm" table="student" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="Id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="60" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="60" />
        </property>
        <property name="subjectid" type="java.lang.Integer">
          <column name="subjectid" length="11"></column>
          </property>
        <many-to-one name="sub" class="com.yourcompany.Subject" column="subjectid" not-null="false" >
        </many-to-one>
    </class>subject配置文件如下:
   <class name="com.yourcompany.Subject" table="subject" catalog="test">
        <composite-id name="id" class="com.yourcompany.SubjectId">
            <key-property name="id" type="java.lang.Integer">
                <column name="id" />
            </key-property>
            <key-property name="subjectName" type="java.lang.String">
                <column name="subjectName" length="60" />
            </key-property>
        </composite-id>
        <set name="students" table="student" inverse="true" cascade="save-update">
         <key column="subjectid"/>
         <one-to-many class="com.yourcompany.LoginForm"></one-to-many>
        </set>
    </class>
看看是哪里出错了?

解决方案 »

  1.   

    Foreign key (FK8FFE823B8197F362:student [subjectid])) must have same number of columns as the referenced primary key (subject [id,subjectName])你的主码没有,你应该先插入subject表,然后再插入loginForm表吧,他的意思是,外码找不到相应的主码匹配。
      

  2.   

    就是说student中的外键subjectid与subject中的id在数量上不一样,肯定是在student中的subjectid在subject表中没有~~~~这在hibernate中是不允许的~~~确定下你插入的student中的subjectid在subject中是否存在~~~~
      

  3.   

    我改过了,可是还是报同样的错,这是我的subject.hbm.xml配置文件:
     <class name="com.yourcompany.Subject" table="subject" catalog="test">
            <composite-id name="id" class="com.yourcompany.Subject">
                <key-property name="id" type="java.lang.Integer">
                    <column name="subjectid" length="11"/>
                </key-property>
                <key-property name="subjectName" type="java.lang.String">
                    <column name="subjectName" length="60" />
                </key-property>
            </composite-id>
            <set name="students" table="student" inverse="true" cascade="save-update">
             <key column="subjectid"/>
             <one-to-many class="com.yourcompany.LoginForm"></one-to-many>
            </set>
        </class>这个是student的配置文件
        <class name="com.yourcompany.Student" table="student" catalog="test">
            <id name="id" type="java.lang.Integer">
                <column name="Id" />
                <generator class="assigned" />
            </id>
            <property name="name" type="java.lang.String">
                <column name="name" length="60" />
            </property>
            <property name="password" type="java.lang.String">
                <column name="password" length="60" />
            </property>
            <property name="subjectid" type="java.lang.Integer">
              <column name="subjectid" length="11"></column>
              </property>
            <many-to-one name="sub" class="com.yourcompany.Subject" column="subjectid" not-null="true" >
            </many-to-one>
    诸位看看,还有什么可能报这样的错?
      

  4.   

    还有一点就是,只是student表中有subjectid这个字段,subject中的id也改成了subjectid,pojo中也改过来了,还是报同样的错,是不是这两个表必须建立主外键的关联关系啊?
      

  5.   

    问题解决了!就是要保证主键字段名和外键字段名完全一致,包括名字和长度!然后重新生成hbm配置文件,就可以解决那个问题了!多谢各位了!
      

  6.   

     <one-to-many class="com.yourcompany.LoginForm" column="subjectid"> </one-to-many>