[regression] unable to persist entity using derby and hibernate 3.5+


when inserting any kind of entity

public class TestObject
private Long id;

private String test;

public TestObject(){}

@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId()
return id;

i get the following error message

org.hibernate.exception.ConstraintViolationException: could not insert: [com.mypackage.TestObject]
>>java.sql.SQLIntegrityConstraintViolationException: Column 'ID' cannot accept a NULL value.
>>org.apache.derby.impl.jdbc.EmbedSQLException: Column 'ID' cannot accept a NULL value.
>>ERROR 23502: Column 'ID' cannot accept a NULL value.
>> at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
>> at org.apache.derby.impl.sql.execute.NormalizeResultSet.normalizeColumn(Unknown Source)
>> at org.apache.derby.impl.sql.execute.NormalizeResultSet.normalizeRow(Unknown Source)
>> at org.apache.derby.impl.sql.execute.NormalizeResultSet.getNextRowCore(Unknown Source)
>> at org.apache.derby.impl.sql.execute.DMLWriteResultSet.getNextRowCore(Unknown Source)

the same code is working fine with versions 3.3.2GA and annotations 3.4.0GA and earlier. i just switched back...


derby and hibernate 3.5+


Max Giesbert
November 2, 2010, 8:17 PM

Just rechecked with Derby and Hibernate 3.6.0 final. Problem still exists. Work-around is to set GenerationType to TABLE.

User known
April 2, 2011, 7:44 AM

I tried with Derby on a whim but as expected the problem still exists - it's a Hibernate issue.

I found an interesting thread while searching earlier, particularly the following message and a number of its follow ups, as these guys basically debugged the Hibernate problem (read about 10-15 messages from this one; you may benefit from reading some of its preceding messages too):

They made a new dialect to solve: org.hisp.dhis.dialect.IdentityDerbyDialect

Any committers interested in fixing this one soon? We have a couple of environments/databases and prefer not using the table generation type for all (one build for all deploys).

Fabien Marsaud
February 22, 2012, 4:27 AM

I believe this issue is not a regression and that it can be closed: Hibernate now simply abides by http://db.apache.org/derby/docs/10.6/ref/rrefsqlj37836.html ID generation.

Let's assume your entities are declared with:
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() { return id; }

At the time of Hibernate 3.3, if your DB was created using the output of configuration.generateSchemaCreationScript(new DerbyDialect()), you must have got some HIBERNATE_UNIQUE_KEY table which acted as a sequence for Hibernate to fill entities' IDs before INSERT. With Hibernate 3.5+ the DDL statements would contain "generated by default as identity" next to each PK declaration.

In other words, now, Hibernate has a good reason not to fill the ID of entities to be inserted: it considers Derby should manage them instead. If you want to keep your Derby IDs as they were (without generated identity), just fill the ID column yourself before INSERT. We could have the same discussion with non-autoincremented MySQL tables PKs.

To fix, you may want to export data, drop contraints+tables, regenerate creation script, check it contains "generated always/by default as identity", create tables, re-insert data, restore constraints, enjoy.

Gail Badner
February 23, 2012, 2:17 AM

Fabien Marsaud, thanks for your explanation and workaround.

Brett Meyer
March 7, 2014, 5:31 PM

Bulk closing rejected tickets in "resolved" state.



Gail Badner


Max Giesbert

Fix versions






Suitable for new contributors


Requires Release Note


Pull Request





Affects versions