-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate PostgreSQL ID
PostPosted: Tue Jun 29, 2010 7:03 am 
Newbie

Joined: Tue Jul 29, 2008 7:01 am
Posts: 9
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


Top
 Profile  
 
 Post subject: Re: Hibernate PostgreSQL ID
PostPosted: Wed Jun 30, 2010 4:13 am 
Newbie

Joined: Tue Jul 29, 2008 7:01 am
Posts: 9
Okay, can anyone advise why might it be the case that the 'create table' statement is issued before the 'create sequence' statement. This would seem to defeat the purpose of being able to specify a sequence generator.

Thanks.


Alan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.