Platform: Postgres 8.3, Hibernate 3.4
I a using Hibernate with Postgres for the first time and am having terrible problems getting ID generation to work correctly. I am trying to define a table such that inserts will work consistently whether made via Hibernate or via some other mechanism, viz. a value need never be specified for the id column.
I've never had any issues using the AUTO stratgey with MySQL or SQL Server however this appears to cause issues with Postgres. Essentially, it seems that Hibernate selects the id from a sequence named hibernate_sequence then does the insert explictly setting this value. This causes problems if data has been inserted by some other means e.g. load some records via db unit and then add a new row via Hibernate - the sequence is still at 1 even though a record exists with this value.
1277809284999|0|5|statement|select nextval ('hibernate_sequence')|select nextval ('hibernate_sequence')
1277809284999|0|5|statement|insert into patterns (name, pattern_id) values (?, ?)|insert into patterns (name, pattern_id) values ('New Pattern', 1)
It would seem what what I require is something like the following where I explicitly define a sequence for a table and assign this to the table.
The issue here is however that the 'create table statement is executed before the create sequence statement and the DDL therefore fails as the sequence referenced in the table DDL does not yet exist.
Code:
@Entity
@Table(name = "patterns")
public class Pattern
{
@Id
@Column(name = "pattern_id", columnDefinition = "integer default nextval('pattern_id_sequence')")
@SequenceGenerator(name="sequenceIDGenerator", sequenceName="pattern_id_sequence")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="sequenceIDGenerator")
private int patternId;
}
Doing something like the following requires that I need to specifically enter an id when adding a record outside of Hibernate.
Code:
@Id
@Column(name = "pattern_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int patternId;
Am I missing something simple here?
Alan