0

Having Hibernate and MSSQL env. I have faced with error: 'Cannot insert the value NULL into column 'comment_id', table 'CHT.dbo.reporter_comments'; 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:

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 NULL into column 'comment_id', table 'CHT.dbo.reporter_comments'; column does not allow nulls. INSERT fails.

Thanks!

1
  • Since I can't think of any other explanation off the bat, I need to ask; are you absolutely sure that you're connecting to the same database from the Java application as you are from the database client? Commented Mar 4, 2013 at 12:01

1 Answer 1

4

Is your comment_id column actually an IDENTITY column? 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.