Field 'id' doesn't have a default value

来源:互联网 发布:监控系统网络拓扑图 编辑:程序博客网 时间:2024/06/05 04:26

在hibernate做测试保存数据的时候,一直报这个错。操作时保存两个表,是一对多的关系,有外键。搞了好几个小时没弄好,后来去网上查,发现原因是主键id是int类型,没有设置自增长,可是我看了xml配置文件里设了<generator class="native" />也没用。有两个方案:

1、打开my.ini,查找

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

修改为

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

然后重启MYSQL

2、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.

第二个方案没看懂,用了第一个方案还是报错。头皮发麻。。。晚上睡觉去了,结果第二天再开机程序正常了。



原创粉丝点击