简体   繁体   English

Hibernate GenericJDBCException:字段“ KeyID”没有默认值

[英]Hibernate GenericJDBCException : Field 'KeyID' doesn't have a default value

Hi I am trying to create one to many relationship with string object with the different key id on same table. 嗨,我正在尝试使用同一表上具有不同键ID的字符串对象创建一对多关系。

But when i insert the data it will show the exception "General error message from server: "Field 'qid' doesn't have a default value". 但是,当我插入数据时,它将显示异常“来自服务器的常规错误消息:“字段'qid'没有默认值”。

How to resolve the problem in hibernate. 如何解决休眠中的问题。 Please refer the below code. 请参考下面的代码。

     <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 code is : 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);

As you map two lists of Answers to the same table, you have to define both key column qid and java_qid as nullable. 将两个Answers列表映射到同一张表时,必须将键列qidjava_qid都定义为可为空。

<key column="qid" not-null="false"></key>
....
<key column="java_qid" not-null="false"></key>

my english is not good but, you may have a column in your database table that is not defined in the entity class code, (self experience), if you touch a table "manually", for example, using a graphic interface like Heidi, and then you make changes in the entity class, is possible that the old column still in the table even if you don't use it in the code. 我的英语不好,但是,如果您“手动”触摸表格(例如,使用Heidi之类的图形界面),则数据库表中可能会有实体类代码中未定义的列((自我经验)),然后您对实体类进行更改,即使您不在代码中使用旧列,也有可能该旧列仍在表中。 So if you have attribute1, attribute2 and attribute3 in your entity class, you going to insert data through them, but there is a fieldX that exist in your table and there is neither a method to set/get or a default value defined, so when you try to insert, this "fieldX" sends an exception: "Field 'fieldX' doesn't have a default value". 因此,如果实体类中有attribute1,attribute2和attribute3,则要通过它们插入数据,但是表中存在fieldX,并且没有设置/获取方法或定义默认值,因此当您尝试插入时,此“ fieldX”会发送异常:“字段'fieldX'没有默认值”。 I fixed the problem supressing the table and re executing hibernate, then fieldX dissapear. 我修复了限制表并重新执行休眠的问题,然后消失了fieldX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM