Field 'id' doesn't have a default value问题解决方法

Field 'id' doesn't have a default value问题解决方法
</pre><pre id="best-content-1001218908" class="best-text mb-10" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: arial, "courier new", courier, 宋体, monospace; line-height: 24px; background-color: rgb(241, 254, 221);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">mysql建了一张表(见代码1),用Hibernate写了个应用。开始时,数据是在mysql的GUI工具里手工输入的</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"> </p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">        运行插入数据的java代码,报异常:<span style="color: rgb(255, 0, 0);">field ‘id' doesn't have a default value<span style="color: rgb(0, 0, 0);">没错啊,我的主键怎么可能有默认值呢?</span></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">第一种解决:</span></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">这个关键字看来是找不到办法了,换成查找hibernate里对主键的设置吧。果然就找到了办法!原来是.hbm.xml文件的设置问题,在里面把主键的属性改为:</span></p><div class="dp-highlighter" style="font-family: Monaco, "DejaVu Sans Mono", "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace; font-size: 12px; width: 766.297px; overflow: auto; padding: 1px; margin-left: 9px; position: relative; word-break: break-all; word-wrap: break-word; line-height: 25.2px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; background-color: rgb(239, 239, 239);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px; font-weight: bold; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left: 3px solid rgb(108, 226, 108); margin: 0px; background-color: rgb(248, 248, 248);">Xml代码  <a target=_blank target="_blank" title="收藏这段代码" style="color: rgb(0, 102, 153); text-decoration: underline; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: left top; background-repeat: no-repeat;"><img class="star" src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px; max-width: 100%;" /></a></div></div><ol start="1" class="dp-xml" style="margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); color: rgb(43, 145, 175); font-size: 1em; line-height: 1.4em; background-color: rgb(255, 255, 255);"><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-top: none; border-right: none; border-bottom: none; border-left: 1px solid rgb(209, 215, 220); list-style: decimal-leading-zero outside; color: rgb(92, 92, 92); line-height: 18px; font-size: 1em; background-color: rgb(250, 250, 250);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">generator</span> <span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">class</span>=<span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"assigned"</span>/<span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">></span>  </span></li></ol></div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"> 经过验证这个是可以的</span><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">        再运行就可以了。因为assigned是指主键是由人工分配的,而native则指主键值由库自动给出。我以前都是想都没想就加上native的,反正也不知道什么意思,而且以前用到要插入数据的表,主键都是“auto increment”的,所以也没碰到问题。这次就暴露了。</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">第二种解决:</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">还可以把数据库中的要插入数据的表主键修改为auto increment也是可以的</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">经验证可行</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">总结一下:就是说如果你没有把主键id 设置为自动增长的,那么如果像<generator class="native"/>这样设置会报上述错误。</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;">所以,可以把表格设置为自动增长的,然后xxx<span style="color: rgb(51, 51, 51); font-family: "comic sans ms", sans-serif; font-size: 14px; line-height: 25.2px; white-space: pre-wrap; background-color: rgb(239, 239, 239);">.hbm.xml文件的设置设为</span><span style="color: rgb(51, 51, 51); font-family: "comic sans ms", sans-serif; font-size: 14px; line-height: 25.2px; white-space: pre-wrap; background-color: rgb(239, 239, 239);"><generator class="native"/></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.2px; background-color: rgb(239, 239, 239);"><span style="font-family: "comic sans ms", sans-serif;"><span style="color: rgb(51, 51, 51); font-family: "comic sans ms", sans-serif; font-size: 14px; line-height: 25.2px; white-space: pre-wrap; background-color: rgb(239, 239, 239);">或者<span style="color: rgb(51, 51, 51); font-family: "comic sans ms", sans-serif; font-size: 14px; line-height: 25.2px; white-space: pre-wrap; background-color: rgb(239, 239, 239);">如果表格设置为非自动增长的,可以把<span style="color: rgb(51, 51, 51); font-family: "comic sans ms", sans-serif; font-size: 14px; line-height: 25.2px; white-space: pre-wrap; background-color: rgb(239, 239, 239);">xxx</span><span style="color: rgb(51, 51, 51); font-family: "comic sans ms", sans-serif; font-size: 14px; line-height: 25.2px; white-space: pre-wrap; background-color: rgb(239, 239, 239);">.hbm.xml文件的设置设为</span><span style="color: rgb(51, 51, 51); font-family: "comic sans ms", sans-serif; font-size: 14px; line-height: 25.2px; white-space: pre-wrap; background-color: rgb(239, 239, 239);"><generator class="assigned"/></span></span></span></span></p>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值