views:

46

answers:

1

I have the following definition for an id field in an entity that is mapped to a table in HSQLDB.

...
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
...

But this does not seem to generate the an unique id; instead an attempt is made to insert null into the column which results in failure. If, I manually create a sequence and generation strategy to use that sequence then the data is persisted as expected.

Doesn't a generation strategy of auto imply that the provider (hibernate in this case) will automatically choose the correct approach and do all the heavy lifting as needed (create sequence, use a native approach or whatever works for that particular platform)? Is my understanding incorrect?

+1  A: 

Doesn't a generation strategy of auto imply that the provider (hibernate in this case) will automatically choose the correct approach and do all the heavy lifting as needed (create sequence, use a native approach or whatever works for that particular platform)? Is my understanding incorrect?

It does in theory (it defaults to IDENTITY with HSQLDB) and it works for me. This begs the following questions:

  • What dialect are you using (just in case)?
  • How did you create the table?
  • Can you show the DDL (activate the logging of org.hibernate.tool.hbm2ddl if required)?
  • How do you insert (through Hibernate's API, right?)?

Here is a sample DDL for an entity Foo when using HSQLDB:

create table Foo (
    id bigint generated by default as identity (start with 1), 
    bar varchar(100),
    primary key (id)
)

I created the table using the HSQL DB manager. Just normal create table address... I had not set the id column as identity in my case - just set it as primary key.

Then you have your answer, use an IDENTITY column.

While Hibernate does choose the right strategy and does generate the appropriate INSERT statements (passing null into the id which is expected to be persisted into an IDENTITY column), it won't create or alter your physical model if you don't use the DDL generation and export capabilities.

Pascal Thivent
* The dialect has been set to org.hibernate.dialect.HSQLDialect* I created the table using the HSQL DB manager. Just normal create table address...* I had created the table manually and not through Hibernate * Yes, through entitymanager's persist.I had not set the id column as identity in my case - just set it as primary key.
calvinkrishy