2012-07-26 46 views
0

我有2个类Student和Class。每个学生只能属于一个班级的一部分。hibernate不会在持久存储器中插入外键标识的值

我尝试使用下面的代码

  for (Class cls : classList) { 
      if (cls.getId().getClassId() == selectedClassId.intValue()) { 
       selectedStudent.setClass(cls); 
      } 
     } 
     HibernateUtil.beginTransaction(); 
     StudentHome stuhome = new StudentHome(); 
     stuhome.persist(selectedStudent); 
     HibernateUtil.commitTransaction(); 

插入学生,但由于某种原因,休眠未在学生表中插入的CLASSID领域。

下面是SQL输出我从休眠

得到
Hibernate: 
select 
    class_.ClassId, 
    class_.EntityId, 
    class_.ClassName as ClassName2_ 
from 
    school.class102 class_ 
where 
    class_.ClassId=? 
    and class_.EntityId=? 

Hibernate: 
insert 
into 
    school.student102 
    (StudentId, ParentId, RouteId, FirstName, LastName, MiddleName, DoB, DoJ, DoL, Status, RegistrationId, EntityId) 
values 
    (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 

Asyou可以看到,CLASSID显然是从插入语句丢失。我不确定Hibernate没有在insert语句中包含classId。

EntityId和RegistrationId是学生的PK和 EntityId和ClassId是Class的PK。 这两个表中都有一个与(EntityId,ClassId)的外键关系。 插入尝试通过一个预定义的EntityId视图

可能是什么问题?

下面是映射

<hibernate-mapping> 
<class name="Student" table="student104" catalog="school"> 
    <composite-id name="id" class="studentId"> 
     <key-property name="registrationId" type="int"> 
      <column name="RegistrationId" /> 
     </key-property> 
     <key-property name="entityId" type="int"> 
      <column name="EntityId" /> 
     </key-property> 
    </composite-id> 
    <many-to-one name="class" class="class" update="false" insert="false" fetch="select"> 
     <column name="ClassId" /> 
     <column name="EntityId" not-null="true" /> 
    </many-to-one> 
    <property name="studentId" type="java.lang.Integer"> 
     <column name="StudentId" /> 
    </property> 
    <property name="parentId" type="java.lang.Integer"> 
     <column name="ParentId" /> 
    </property> 
    <property name="routeId" type="java.lang.Integer"> 
     <column name="RouteId" /> 
    </property> 
    <property name="firstName" type="string"> 
     <column name="FirstName" length="45" /> 
    </property> 
    <property name="lastName" type="string"> 
     <column name="LastName" length="45" /> 
    </property> 
    <property name="middleName" type="string"> 
     <column name="MiddleName" length="45" /> 
    </property> 
    <property name="doB" type="date"> 
     <column name="DoB" length="10" /> 
    </property> 
    <property name="doJ" type="date"> 
     <column name="DoJ" length="10" /> 
    </property> 
    <property name="doL" type="date"> 
     <column name="DoL" length="10" /> 
    </property> 
    <property name="status" type="string"> 
     <column name="Status" length="45" /> 
    </property> 
</class> 

<hibernate-mapping> 
<class name="class" table="class102" catalog="school"> 
    <composite-id name="id" class="classId"> 
     <key-property name="classId" type="int"> 
      <column name="ClassId" /> 
     </key-property> 
     <key-property name="entityId" type="int"> 
      <column name="EntityId" /> 
     </key-property> 
    </composite-id> 
    <property name="className" type="string"> 
     <column name="ClassName" length="45" not-null="true" /> 
    </property> 
    <set name="students" table="student104" inverse="true" lazy="true" fetch="select"> 
     <key> 
      <column name="ClassId" /> 
      <column name="EntityId" not-null="true" /> 
     </key> 
     <one-to-many class="student" /> 
    </set> 
</class> 

+2

提供您的映射 – Pokuri 2012-07-26 13:47:28

+0

@Pokuri感谢您抽出时间,我现在添加了映射。 – 2012-07-26 14:02:59

+0

你好,我有完全一样的问题。你有能力解决这个问题吗?谢谢 – aumanets 2013-03-20 09:15:02

回答

0

如果你看看你的学生

的映射
<many-to-one name="class" class="class" update="false" insert="false" fetch="select"> 
     <column name="ClassId" /> 
     <column name="EntityId" not-null="true" /> 
</many-to-one> 

你说不要插入或更新的关系成学生。如果您认为一旦被分配到班级的学生不能修改,则设置update="false" insert="true"其他可以稍后修改,然后设置update="true" insert="true"。并且这两个属性值都是默认值(如果您从映射中忽略它们)true

+1

我试着把两个都设置为'true',但得到了一个Mapping异常。它说..重复实体映射列:学生列:EntityId(应映射与插入=“假”更新=“假”)..它看起来像一个两个表上有一个复合PK的问题..我我不确定你。 – 2012-07-26 16:51:27