我尝试将映射写入表。我决定用于表测试的id和versionId - composite-id。但是在表test_question中,我有复合id,包括question_id,test_id和versionId。我无法理解,因为我可以使用一个复合ID id-versionId作为其他复合ID。
我有下一个和平的脚本。
Create Table Test
(
id int unsigned not null,
versionId int unsigned not null,
test_text varchar(200) not null,
subject_id int unsigned not null,
primary key (id, versionId),
Foreign key (subject_id) REferences Subject(id)
On delete cascade on update cascade
)
Engine InnoDB CHARACTER SET utf8;
Create Table Question
(
id varchar(36) not null,
question_text varchar(200) not null,
primary key(id)
)
Engine InnoDB CHARACTER SET utf8;
Create table Test_Question
(
test_id int unsigned not null,
question_id varchar(36) not null,
versionId int unsigned not null,
primary key(test_id, question_id, versionId),
Foreign key(test_id,versionId) References Test(id, versionId),
Foreign key(question_id) References Question(id)
)
Engine InnoDB CHARACTER SET utf8;
我的映射
Test.hbm.xml
<hibernate-mapping>
<class name="by.bsuir.testapp.model.Test" table="TEST">
<composite-id>
<key-property name="testId" type="long" column="TEST_ID" />
<key-property name="versionId" type="long" column="VERSION_ID" />
<generator class="assigned"/>
</composite-id>
<property name="testName" type="string">
<column name="TESTNAME" />
</property>
<many-to-one name="subject" class="by.bsuir.testapp.model.Subject"
fetch="join">
<column name="SUBJECT_ID" />
</many-to-one>
</class>
</hibernate-mapping>
和TestQuestion.hbm.xml
<hibernate-mapping>
<class name="by.bsuir.testapp.model.TestQuestion" table="TEST_QUESTION">
<composite-id>
<key-property name="test" type="long" column="TEST_ID" />
<key-property name="question" type="long" column="QUESTION_ID" />
<generator class="assigned" />
</composite-id>
</class>
</hibernate-mapping>
表TestQuestion中应该是什么id?我的意思是QUESTION_ID-TEST_ID-VERSION_ID是整个复合id。