0

我尝试将映射写入表。我决定 id 和 versionId - 用于表测试的复合 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;

还有我的映射

测试.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。

4

0 回答 0