Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi All,
Hibernate version: 3.3.1
Hibernate annotation version: 3.4.0
Hibernate tools version: 3.2.4.beta1
Java Version: 1.5
Name and version of the database you are using: postgresql 8.3.5-2
I am writing an application that needs to support multiple databases. I am trying to add support for PostgreSQL, however I have run into a problem. I am also using Hibernate tools ant task hbm2dll to generate all my db schema's. I am using annotations to map my classes to tables (see below)
Code:
...
@Entity
@AccessType("field")
@Table(name=Country.TABLE_COUNTRY)
public class Country implements Comparable<Country>
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(unique=true,nullable=false,length=7)
@Enumerated(EnumType.STRING)
private CountryCode code;
@Column(nullable=false)
private String name;
public static final String TABLE_COUNTRY = "country";
...
}
This results in the following schema definition:
Code:
...
create table country (
id int8 not null,
code varchar(7) not null unique,
name varchar(255) not null,
primary key (id)
);
...
create sequence hibernate_sequence;
...
However what I need to be generated is the following. Note the type of the id field and the individual sequences:
Code:
...
create table country (
id bigserial not null,
code varchar(7) not null unique,
name varchar(255) not null,
primary key (id)
);
...
create sequence country_id_seq;
...
I have solved the individual sequences per table using the idea's suggested here:
http://www.hibernate.org/296.html
But I notice that the default native Id generator class is SequenceGenerator. This works fine for me when I have manually defined my id column to be a serial or bigserial in PostgreSQL. However when using hbm2ddl, the only generator that creates a field as serial/bigserial is the IdentityGenerator. If I explicitly set that as my generation strategy then I get errors later on. Also I think the sequnce generator works well with PostgreSQL and I dont really want to deviate from that if possible.
So finally my questions.
Is the behaviour described above intentional?
Is there a way to use the native Id generator for Postgres (i.e. SequenceGenerator) and be able to generate my schema ddl where my id fields are of type serial/bigserial?
Is there a way I can specify the name of the sequence for my id fields?
Cheers,
Ben