简体   繁体   English

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

[英]Hibernate + MSSQL, Cannot insert the value NULL into column

Having Hibernate and MSSQL env. 具有Hibernate和MSSQL env。 I have faced with error: 'Cannot insert the value NULL into column 'comment_id', table 'CHT.dbo.reporter_comments'; 我遇到了错误:'无法将值NULL插入表'CHT.dbo.reporter_comments'的列'comment_id'中; column does not allow nulls. 列不允许为空。 INSERT fails.' 插入失败。 Hibernate configuration: 休眠配置:

<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' column has 'identity' property in db: if i do insertion using db client: 'comment_id'列在数据库中具有'identity'属性:如果我确实使用数据库客户端插入:

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

it works perfect for me. 它对我来说很完美。

Hibernate logs: 休眠日志:

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 DEBUG hibernate.SQL-插入REPORTER_COMMENTS(REPORTERID,COM​​MENT,COMMENT_DATE,COMMENT_BY)值(?,?,?,?)DEBUG hibernate.jdbc.AbstractBatcher-准备语句DEBUG hibernate.persister.entity.AbstractEntityPersister-脱水实体:[- -。ReporterComments#] DEBUG hibernate.type.LongType-将'15119'绑定到参数:1 DEBUG hibernate.type.StringType-将'test'绑定到参数:2 DEBUG hibernate.type.TimestampType-绑定'2013-03-04 06:13:02'到参数:3 DEBUG hibernate.type.StringType-将'dw'绑定到参数:4 DEBUG hibernate.jdbc.AbstractBatcher-即将关闭PreparedStatement(open PreparedStatements:1,全局:1)DEBUG hibernate.jdbc .AbstractBatcher-结束语句DEBUG hibernate.util.JDBCExceptionReporter-无法插入:[-.. ReporterComments] [插入到REPORTER_COMMENTS(REPORTERID,COM​​MENT,COMMENT_DATE,COMMENT_BY)值(?,?,?,?)中.SQLException:[BEA] [SQLServer JDBC驱动程序] [SQLServer]无法插入值 NULL into column 'comment_id', table 'CHT.dbo.reporter_comments'; 在表'CHT.dbo.reporter_comments'的'comment_id'列中为NULL; column does not allow nulls. 列不允许为空。 INSERT fails. INSERT失败。

Thanks! 谢谢!

Is your comment_id column actually an IDENTITY column? 您的comment_id列实际上是IDENTITY列吗? If it isn't, then that's your problem since Hibernate won't include the comment_id column in an insert statement when using an identity generator, relying on the database to generate the value. 如果不是,那是您的问题,因为使用身份生成器时,Hibernate不会在insert语句中包括comment_id列,而是依靠数据库来生成值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM