gpt4 book ai didi

java - Hibernate + MSSQL,无法将值 NULL 插入列中

转载 作者:行者123 更新时间:2023-12-01 23:58:57 25 4
gpt4 key购买 nike

拥有 Hibernate 和 MSSQL 环境。我遇到过错误:'无法将 NULL 值插入表'CHT.dbo.reporter_comments'的'comment_id'列;列不允许为空。插入失败。 hibernate 配置:

<id unsaved-value = "null" type = "int" name="commentId" column="comment_id">
<generator class="identity"/>
</id>

<property name="reporterId" column="REPORTERID" />
<property name="adminComments" column="COMMENT" />
<property name="commentDate" column="COMMENT_DATE"/>
<property name="commentBy" column="COMMENT_BY"/>

“comment_id”列在数据库中具有“identity”属性:如果我使用数据库客户端进行插入:

insert into REPORTER_COMMENTS (REPORTERID, COMMENT, COMMENT_DATE, COMMENT_BY) 
values (15119, 'test', '2013-03-04 05:45:25', 'admin');

它非常适合我。

hibernate 日志:

DEBUG hibernate.SQL - insert into REPORTER_COMMENTS (REPORTERID, COMMENT, COMMENT_DATE, COMMENT_BY) values (?, ?, ?, ?) DEBUG hibernate.jdbc.AbstractBatcher - preparing statement DEBUG hibernate.persister.entity.AbstractEntityPersister - Dehydrating entity: [---.ReporterComments#] DEBUG hibernate.type.LongType - binding '15119' to parameter: 1 DEBUG hibernate.type.StringType - binding 'test' to parameter: 2 DEBUG hibernate.type.TimestampType - binding '2013-03-04 06:13:02' to parameter: 3 DEBUG hibernate.type.StringType - binding 'dw' to parameter: 4 DEBUG hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) DEBUG hibernate.jdbc.AbstractBatcher - closing statement DEBUG hibernate.util.JDBCExceptionReporter - could not insert: [---.ReporterComments] [insert into REPORTER_COMMENTS (REPORTERID, COMMENT, COMMENT_DATE, COMMENT_BY) values (?, ?, ?, ?)] java.sql.SQLException: [BEA][SQLServer JDBC Driver][SQLServer]Cannot insert the value NULL into column 'comment_id', table 'CHT.dbo.reporter_comments'; column does not allow nulls. INSERT fails.

谢谢!

最佳答案

您的 comment_id 列实际上是 IDENTITY 列吗?如果不是,那么这就是您的问题,因为在使用身份生成器时,Hibernate 不会在插入语句中包含 comment_id 列,而是依赖数据库来生成值。

关于java - Hibernate + MSSQL,无法将值 NULL 插入列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15200555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com