赞
踩
Field 'id' doesn't have a default value问题解决方法
</pre><pre class="best-text mb-10" id="best-content-1001218908" style="padding: 0px; color: rgb(51, 51, 51); line-height: 24px; font-family: arial,'courier new',courier,宋体,monospace; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-word-wrap: break-word; background-color: rgb(241, 254, 221);" name="code"><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"><span style="font-size:14px;"><span style="font-family:'comic sans ms',sans-serif;">突然想温习温习对数据库的读写,于是就用mysql建了一张单独的表(见代码1),用Hibernate写了个应用,可以正常查询、修改数据了。开始时,数据是在mysql的GUI工具里手工输入的,嫌烦,于是就想到用程序批量生成一些,于是麻烦也跟着来了……</span></span></p><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"> 代码1</p><div class="dp-highlighter" style="padding: 1px; width: 766.29px; line-height: 25.2px; overflow: auto; font-family: Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; margin-left: 9px; -ms-word-break: break-all; -ms-word-wrap: break-word; background-color: rgb(239, 239, 239);"><div class="bar"><div class="tools" style="margin: 0px; padding: 3px; font-weight: bold;">Sql代码 <a target=_blank title="收藏这段代码" style="color: rgb(0, 102, 153); text-decoration: underline;" target="_blank"><img class="star" style="border: 0px currentColor;" alt="收藏代码" src="http://www.iteye.com/images/icon_star.png" /></a></div></div><ol class="dp-sql" style="margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); color: rgb(43, 145, 175); line-height: 1.4em; font-size: 1em; background-color: rgb(255, 255, 255);" start="1"><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; line-height: 18px; font-size: 1em; border-left-color: rgb(209, 215, 220); border-left-width: 1px; border-left-style: solid; background-color: rgb(250, 250, 250);"><span style="color:black;"><span class="keyword" style="color:#7f055;font-weight: bold;">CREATE</span> <span class="keyword" style="color:#7f055;font-weight: bold;">TABLE</span> `stitp`.`Ticket` ( </span></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; line-height: 18px; font-size: 1em; border-left-color: rgb(209, 215, 220); border-left-width: 1px; border-left-style: solid; background-color: rgb(250, 250, 250);"><span style="color:black;"> `id` <span class="keyword" style="color:#7f055;font-weight: bold;">int</span>(11) <span class="op" style="color:#808080;">NOT</span> <span class="op" style="color:#808080;">NULL</span>, </span></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; line-height: 18px; font-size: 1em; border-left-color: rgb(209, 215, 220); border-left-width: 1px; border-left-style: solid; background-color: rgb(250, 250, 250);"><span style="color:black;"> `state` tinyint(1) <span class="op" style="color:#808080;">NOT</span> <span class="op" style="color:#808080;">NULL</span>, </span></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; line-height: 18px; font-size: 1em; border-left-color: rgb(209, 215, 220); border-left-width: 1px; border-left-style: solid; background-color: rgb(250, 250, 250);"><span style="color:black;"> `grade` <span class="keyword" style="color:#7f055;font-weight: bold;">int</span>(2) <span class="op" style="color:#808080;">NOT</span> <span class="op" style="color:#808080;">NULL</span>, </span></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; line-height: 18px; font-size: 1em; border-left-color: rgb(209, 215, 220); border-left-width: 1px; border-left-style: solid; background-color: rgb(250, 250, 250);"><span style="color:black;"> <span class="keyword" style="color:#7f055;font-weight: bold;">PRIMARY</span> <span class="keyword" style="color:#7f055;font-weight: bold;">KEY</span> (`id`) </span></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; line-height: 18px; font-size: 1em; border-left-color: rgb(209, 215, 220); border-left-width: 1px; border-left-style: solid; background-color: rgb(250, 250, 250);"><span style="color:black;">) ENGINE=InnoDB <span class="keyword" style="color:#7f055;font-weight: bold;">DEFAULT</span> CHARSET=gbk </span></li></ol></div><span style="font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;line-height: 25.2px; background-color: rgb(239, 239, 239);"> </span><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"><span style="font-size: 14px;"><span style="font-family:'comic sans ms',sans-serif;"> 运行插入数据的java代码,报异常:<span style="color:#ff00;">field ‘id' doesn't have a default value<span style="color:#000000;">没错啊,我的主键怎么可能有默认值呢?不理解。谷歌了一下这个异常,一大坨结果,总结一下就是那么两种方法:</span></span></span></span></p><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);">解决方法一: 打开my.ini,查找 sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 修改为 sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 然后重启MYSQL</p><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"> 解决方法二: 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.</p><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"><span style="font-size:14px;"><span style="font-family:'comic sans ms',sans-serif;"> 都试了,没用,而且我是Fedora,没有ini文件,而是cnf文件(我的my.cnf在/etc下面)</span></span></p><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"><span style="font-size:14px;"><span style="font-family:'comic sans ms',sans-serif;">这个关键字看来是找不到办法了,换成查找hibernate里对主键的设置吧。果然就找到了办法!原来是.hbm.xml文件的设置问题,在里面把主键的属性改为:</span></span></p><div class="dp-highlighter" style="padding: 1px; width: 766.29px; line-height: 25.2px; overflow: auto; font-family: Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; margin-left: 9px; -ms-word-break: break-all; -ms-word-wrap: break-word; background-color: rgb(239, 239, 239);"><div class="bar"><div class="tools" style="margin: 0px; padding: 3px; font-weight: bold;">Xml代码 <a target=_blank title="收藏这段代码" style="color: rgb(0, 102, 153); text-decoration: underline;" target="_blank"><img class="star" style="border: 0px currentColor;" alt="收藏代码" src="http://www.iteye.com/images/icon_star.png" /></a></div></div><ol class="dp-xml" style="margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); color: rgb(43, 145, 175); line-height: 1.4em; font-size: 1em; background-color: rgb(255, 255, 255);" start="1"><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; line-height: 18px; font-size: 1em; border-left-color: rgb(209, 215, 220); border-left-width: 1px; border-left-style: solid; background-color: rgb(250, 250, 250);"><span style="color:black;"><span class="tag" style="color:#06699;font-weight: bold;"><</span><span class="tag-name" style="color:#06699;font-weight: bold;">generator</span> <span class="attribute" style="color:red;">class</span>=<span class="attribute-value" style="color:blue;">"assigned"</span>/<span class="tag" style="color:#06699;font-weight: bold;">></span> </span></li></ol></div><span style="font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;line-height: 25.2px; background-color: rgb(239, 239, 239);"> </span><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"><span style="font-size:14px;"><span style="font-family:'comic sans ms',sans-serif;"> 再运行就可以了。因为assigned是指主键是由人工分配的,而native则指主键值由库自动给出。我以前都是想都没想就加上native的,反正也不知道什么意思,而且以前用到要插入数据的表,主键都是“auto increment”的,所以也没碰到问题。这次就暴露了。</span></span></p><p style="line-height: 25.2px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica,Tahoma,Arial,sans-serif; font-size: 14px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(239, 239, 239);"><span style="font-size:14px;"><span style="font-family:'comic sans ms',sans-serif;">总结一下:其实就是缺乏对Hibernate的全面理解,没有完全掌握它的各种设置。因为当初学hibernate也是抱着"需求"学的,而不是正儿八经学习它的,没有完全理解各种设置就上手了。</span></span></p>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。