【问题标题】:hibernate will not insert value of foreign key id on persisthibernate 不会在持久化时插入外键 id 的值
【发布时间】:2012-07-26 13:42:19
【问题描述】:

我有 2 个班级,学生和班级。每个学生只能是一个班级的一部分。

我正在尝试使用以下代码插入学生

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

但由于某种原因,Hibernate 没有在 Student 表中插入 classId 字段。

下面是我从 Hibernate 得到的 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
    (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

如您所见,插入语句中显然缺少 classId。我不确定 Hibernate 是否没有在插入语句中包含 classId。

EntityId 和 RegistrationId 是 Student 的 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>

【问题讨论】:

  • @Pokuri 感谢您抽出宝贵时间,我现在已经添加了映射。
  • 你好,我也有同样的问题。你能解决这个问题吗?谢谢

标签: hibernate


【解决方案1】:

如果您查看学生的映射

<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

【讨论】:

  • 我尝试将两者都设置为“true”,但出现映射异常。它说..实体映射中的重复列:学生列:EntityId(应该使用 insert="false" update="false" 进行映射)..看起来在两个表上都有一个复合 PK 的问题..我我不确定你。
猜你喜欢
  • 2013-03-03
  • 2011-12-23
  • 1970-01-01
  • 2021-12-31
  • 2014-03-26
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多