`
smartzxy
  • 浏览: 200493 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Field Doesn't Have a Default Value

阅读更多

       突然想温习温习对数据库的读写,于是就用mysql建了一张单独的表(见代码1),用Hibernate写了个应用,可以正常查询、修改数据了。开始时,数据是在mysql的GUI工具里手工输入的,嫌烦,于是就想到用程序批量生成一些,于是麻烦也跟着来了……

    代码1

CREATE TABLE  `stitp`.`Ticket` (
  `id` int(11) NOT NULL,
  `state` tinyint(1) NOT NULL,
  `grade` int(2) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
 

        运行插入数据的java代码,报异常:field ‘id' doesn't have a default value没错啊,我的主键怎么可能有默认值呢?不理解。谷歌了一下这个异常,一大坨结果,总结一下就是那么两种方法:

解决方法一:
打开my.ini,查找
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
修改为
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
然后重启MYSQL


解决方法二:
MySQL 5 uses a strict mode which needs to be disabled.
In Windows, Goto Start-->Programs-->MySQL->MySQL Instance Config Wizard. Follow through the Reconfigure Instance option-->Detailed Configuration-->Continue Next a few screens. At the bottom under Enable TCP/IP option there is 'Enable Strict Mode'. Deslect this option (no tick). Save changes and MySQL will restart.

       都试了,没用,而且我是Fedora,没有ini文件,而是cnf文件(我的my.cnf在/etc下面)

这个关键字看来是找不到办法了,换成查找hibernate里对主键的设置吧。果然就找到了办法!原来是.hbm.xml文件的设置问题,在里面把主键的属性改为:

<generator class="assigned"/>
 

        再运行就可以了。因为assigned是指主键是由人工分配的,而native则指主键值由库自动给出。我以前都是想都没想就加上native的,反正也不知道什么意思,而且以前用到要插入数据的表,主键都是“auto increment”的,所以也没碰到问题。这次就暴露了。

总结一下:其实就是缺乏对Hibernate的全面理解,没有完全掌握它的各种设置。因为当初学hibernate也是抱着"需求"学的,而不是正儿八经学习它的,没有完全理解各种设置就上手了。

分享到:
评论
1 楼 kjj 2009-05-30  
引用

总结一下:其实就是缺乏对Hibernate的全面理解,没有完全掌握它的各种设置。因为当初学hibernate也是抱着"需求"学的,而不是正儿八经学习它的,没有完全理解各种设置就上手了。



再给你总结一下,不是hibernate问题,是mysql问题,数据库主键是必须的字段!!

相关推荐

    解决 Field &#039;ssl_cipher&#039; doesn&#039;t have a default value的错误

    ### 解决Field 'ssl_cipher' doesn't have a default value的错误 #### 背景介绍 在使用MySQL数据库的过程中,可能会遇到一个特定的错误:“Field 'ssl_cipher' doesn't have a default value”。这个错误通常发生...

    MySQL之Field‘***’doesn’t have a default value错误解决办法

    在使用MySQL时,可能会遇到“Field '***' doesn't have a default value”的错误,这个问题通常发生在尝试插入数据到数据库表时,某个字段没有提供值,而该字段在表定义中被设定为非空(NOT NULL)且没有设置默认值...

    S7A驱动720版本

    - The field "Default Gateway" has been added to the NetLink's configuration property page - The string write from the OPC Server sometimes failed . This bug has been fixed - After driver uninstall...

    php.ini-development

    directive because it is not set or is mistyped, a default value will be used. ; The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one ; of the INI constants (On, Off, True, ...

    Sakemail

    .- Reduced the line sleep to 30 (tell me if this value doesn‘t work for you).27/4/981.7.0- Fixed a memory leak, thanks to Don Higgins.- Moved the string esErrorInFormatOfMsg to a property of SakPOP.-...

    Delphi7.1 Update

    * TClientDataSet doesn‘t save data to file when FileName is set and there is no existing file on disk (Quality Central 2307). * Using the Delphi 7 version of midas.dll to open an XML file that was ...

    数位板压力测试

    The device must have a point-ing ap¬para¬tus or method (such as a stylus, or a finger touching a touch pad), called the cursor, that de¬fines the current position. The cursor must be able to ...

    微软内部资料-SQL性能优化5

    This uniqueifier is a 4-byte value added as an additional sort key to only the rows that have duplicates of their primary sort key. You can see this extra value if you use DBCC PAGE to look at the ...

    EhLib 9.1.024

    Have a special column to show Field Captions Can customize inplace editor and data of the cell like in DBGridEh. TDBLookupComboboxEh component Provides all functionality of TDBLookupCombobox and ...

    计算机网络第六版答案

    Third, if ISPs decide to charge more money to highly profitable content providers (in countries where net neutrality doesn't apply), the content providers can avoid these extra payments. 16. The ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    link ▶Don't use an #include when a forward declaration would suffice. When you include a header file you introduce a dependency that will cause your code to be recompiled whenever the header file ...

    ehlib_vcl_src_9_3.26

    Have a special column to show Field Captions Can customize inplace editor and data of the cell like in DBGridEh. TDBLookupComboboxEh component Provides all functionality of TDBLookupCombobox and ...

    acpi控制笔记本风扇转速

    is a region field. A buffer object is also allowed as the source operand. BZ 480 Fixed a problem where the Load ASL operator allowed the source operand to be an operation region of any type. It is ...

    EhLib 8.0 Build 8.0.023 Pro Edition FullSource for D7-XE8

    Have a special column to show Field Captions Can customize inplace editor and data of the cell like in DBGridEh. TDBLookupComboboxEh component Provides all functionality of TDBLookupCombobox and ...

    EhLib5.0.13 最新的ehlib源码

    with a scrollable list and can display and edit a field in a dataset or can works as non data-aware combo edit control. TDBNumberEditEh component represents a single-line number edit control that ...

    EhLib 6.3 Build 6.3.176 Russian version. Full source included.

    Have a special column to show Field Captions Can customize inplace editor and data of the cell like in DBGridEh. TDBLookupComboboxEh component Provides all functionality of TDBLookupCombobox and ...

    Java邮件开发Fundamentals of the JavaMail API

    capability isn't supported by a chosen protocol, the JavaMail API doesn't magically add the capability on top of it. (As you'll soon see, this can be a problem when working with POP.) SMTP The ...

    mysql单选题目111

    5. 在执行添加数据时出现 “Field 'name' doesn't have a default value” 错误,可能导致错误的原因是( )。 a) INSERT 语句出现了语法问题 b) name 字段没有指定默认值,且添加了 NOT NULL 约束 c) name 字段...

    apache-maven-3.0.2-bin

    * [MNG-4941] - PluginDescriptorBuilder doesn't populate expression/default-value fields for mojo parameters * [MNG-4952] - [regression] RELEASE field of repository metadata is not updated upon ...

Global site tag (gtag.js) - Google Analytics