我正在尝试在两个表之间创建一对多的关系。 TestCase有一个名为Type
的FK基本上很多testCases可以有一种类型,但testCase只能有一种类型。
我在这里缺少什么?
但我得到了
字段列表中的未知列idtestType
模型
public class TestCase {
private int id;
private String name;
private TestType type;
private byte[] data;
private String creationDate;
private String createdBy;
public TestCase() {
}
public TestCase(String name, TestType type, byte[] data, String creationDate, String createdBy) {
this.name = name;
this.type = type;
this.data = data;
this.creationDate = creationDate;
this.createdBy = createdBy;
}
TestCase.xml
<hibernate-mapping>
<class name="com.atp.Model.TestCases.TestCase" table="testCases">
<meta attribute="class-description">
This class contains the testCases details.
</meta>
<id name="id" type="int" column="idtestCase">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<many-to-one name="type" class="com.atp.Model.TestCases.TestType" fetch="select">
<column name="idtestType"/>
</many-to-one>
<property name="data" column="data" type="binary"/>
<property name="creationDate" column="creationDate" type="string"/>
<property name="createdBy" column="createdBy" type="string"/>
</class>
</hibernate-mapping>
模型
public class TestType {
private int id;
private String desc;
private Set<TestCase> testCases = new HashSet<>(0);
public TestType() {
}
public TestType(String desc, Set<TestCase> testCases) {
this.desc = desc;
this.testCases = testCases;
}
TestType.xml
<hibernate-mapping>
<class name="com.atp.Model.TestCases.TestType" table="testType">
<meta attribute="class-description">
This class contains the testCases details.
</meta>
<id name="id" type="int" column="idtestType">
<generator class="native"/>
</id>
<property name="desc" column="desc" type="string"/>
<set name="testCases">
<key>
<column name="idtestType" />
</key>
<one-to-many class="com.atp.Model.TestCases.TestCase" />
</set>
</class>
</hibernate-mapping>
尝试将其添加到数据库
public static void addTestCase(String testName, String type, MultipartFile file, String createdBy) {
byte[] data;
Date date = new Date();
final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String creationDate = sdf.format(date);
TestType testType = new TestType();
testType.setDesc(type);
try {
data = file.getBytes();
TestCase testCase = new TestCase(testName,testType,data,creationDate,createdBy);
testType.getTestCases().add(testCase);
Database.addToDatabase(testCase);
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
你错过了
idtestType
TestCase表中的列。
或者,如果要在现有表中使用type
列,则TestCase.xml-Mapping错误,应该
<many-to-one name="type" class="com.atp.Model.TestCases.TestType" fetch="select">
<column name="type"/>
</many-to-one>
(列是“type”而不是“idtestType”)