我有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
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Asyou可以看到,insert语句中明显缺少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>
答案 0 :(得分: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"
。默认情况下,这两个属性值(如果您从映射中省略它们)仅{。}}。