[英]Hibernate GenericJDBCException : Field 'KeyID' doesn't have a default value
嗨,我正在嘗試使用同一表上具有不同鍵ID的字符串對象創建一對多關系。
但是,當我插入數據時,它將顯示異常“來自服務器的常規錯誤消息:“字段'qid'沒有默認值”。
如何解決休眠中的問題。 請參考下面的代碼。
<class name="com.db.HRQuestion" table="HRQuestion">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="qname"></property>
<list name="answers" table="answer">
<key column="qid"></key>
<index column="type"></index>
<element column="answer" type="string"></element>
</list>
</class>
<class name="com.db.JavaQuestion" table="javaQuestion">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="qname"></property>
<list name="answers" table="answer">
<key column="java_qid"></key>
<index column="type"></index>
<element column="answer" type="string"></element>
</list>
</class>
Java代碼是:
JavaQuestion javaQuestion= new JavaQuestion();
javaQuestion.setQname("What is meant by java?");
ArrayList<String> javaanswerlist=new ArrayList<String>();
javaanswerlist.add("java is Object oriented programming ");
javaanswerlist.add("java is a platform independent");
javaQuestion.setAnswers(javaanswerlist);
HRQuestion hrquestion=new HRQuestion();
hrquestion.setQname("Hr Question one");
ArrayList<String> list2=new ArrayList<String>();
list2.add("My profile .....");
list2.add("My objetcive...");
hrquestion.setAnswers(list1);
session.save(javaQuestion);
session.save(hrquestion);
將兩個Answers列表映射到同一張表時,必須將鍵列qid
和java_qid
都定義為可為空。
<key column="qid" not-null="false"></key>
....
<key column="java_qid" not-null="false"></key>
我的英語不好,但是,如果您“手動”觸摸表格(例如,使用Heidi之類的圖形界面),則數據庫表中可能會有實體類代碼中未定義的列((自我經驗)),然后您對實體類進行更改,即使您不在代碼中使用舊列,也有可能該舊列仍在表中。 因此,如果實體類中有attribute1,attribute2和attribute3,則要通過它們插入數據,但是表中存在fieldX,並且沒有設置/獲取方法或定義默認值,因此當您嘗試插入時,此“ fieldX”會發送異常:“字段'fieldX'沒有默認值”。 我修復了限制表並重新執行休眠的問題,然后消失了fieldX。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.