-->
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.  [ 4 posts ] 
Author Message
 Post subject: Postgres 'nextval' and 'serial' question
PostPosted: Mon Dec 10, 2007 1:36 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:30 pm
Posts: 46
Location: Fortaleza, Brazil
Hibernate version:
3.2

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.wazollc.alphatheory.hibernate.bo">
<class name="RoleBO"
table="`ROLE`"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
lazy="true"
batch-size="6"
>
<id name="id" type="java.lang.Long" column="`roleID`">
<generator class="native">
<param name="sequence">at_sequence</param>
</generator>
</id>
<version name="version" type="java.lang.Long" access="field" column="OBJ_VERSION"/>

<property name="created" column="`created`" type="timestamp"
not-null="false" />
<property name="modifiedUserFullName"
column="`modifiedUserFullName`" type="java.lang.String"
not-null="false" />
<property name="modified" column="`modified`" type="timestamp"
not-null="false" />
<property name="roleName" column="`roleName`"
type="java.lang.String" not-null="false" />
<property name="createdUserID" column="`createdUserID`"
type="java.lang.Long" not-null="false" />
<property name="modifiedUserID" column="`modifiedUserID`"
type="java.lang.Long" not-null="false" />
<property name="createdUserFullName"
column="`createdUserFullName`"
type="java.lang.String" not-null="false"
/>


</class>
</hibernate-mapping>

Name and version of the database you are using:
postgresql-8.1.9

Hi all, I'm migrating our production hibernate app from sql server to postgres. I've searched the forums a bunch and I'm unable to explain why the generated ddl doesn't follow this form, as shown from the postgres docs:

http://www.postgresql.org/docs/8.2/stat ... table.html

Code:
CREATE TABLE distributors (
     did    integer PRIMARY KEY DEFAULT nextval('serial'),
     name   varchar(40) NOT NULL CHECK (name <> '')
);


The hibernate mapping document I use above with hibernate tools doesn't create a ddl with 'serial' or most importantly, doesn't have nextval. It does, however, have a sequence.

Code:
create table "ROLE" ("roleID" int8 not null, OBJ_VERSION int8 not null, "created" timestamp, "modifiedUserFullName" varchar(255), "modified" timestamp, "roleName" varchar(255), "createdUserID" int8, "modifiedUserID" int8, "createdUserFullName"
varchar(255), primary key ("roleID"));
...
create sequence at_sequence;


Is nextval as part of the 'create table' not mandatory? Is int8 ok to use as the PK instead of 'serial'? Will hibernate call nextval with my sequence at runtime on new inserts and is that all I need? Sorry if these are more suited as postgres questions, but the hibernate generated DDL is different than what I'm reading from the postgres docs.

Thanks for any clarification!
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 6:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I have an application dealing with both databases and we decided to use sequences as the base for key generation rather than serial keys. By default hibernate postgres dialect is setup for sequences. You could re-work your own version of the dialect to (possibly) make it work.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 14, 2007 3:11 am 
Newbie

Joined: Tue Nov 27, 2007 6:11 am
Posts: 12
Using a sequence is a flexible and standard way to get an auto-generated column key, so hibernate is not misbehaving.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 16, 2007 7:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
True but many databases support only serial key generaion and some support both - in this case you have a choice. Hibernate by default for postgres uses sequences (which is the best option in my personal view) so the question was 'How to make it work for serial key mode?'. Modify the dialect is the answer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.