13

I have my entity class mapped like below:

@Entity
@Audited
@Table(name="messages_locale")
public class Locale {

    @Id
    @GeneratedValue
    @Getter @Setter //Project Lombok's annotations, equal to generated getter and setter method
    private int id;
        (...)

I create clean new database ,and properties:

< prop key="hibernate.hbm2ddl.auto" >create < /prop>

WHY THE HELL (Sorry, almost two days wasted on this bug) after created database, i got a sequence in my postgres db?:

CREATE SEQUENCE hibernate_sequence
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 2
  CACHE 1;
ALTER TABLE hibernate_sequence
  OWNER TO postgres;

I dont want to have a sequence, I want to have just auto increment auto generated values..

2 Answers 2

29

I think the accepted answer from Petar is not correct, or not correct any longer. The auto-increment in Postgres is handled through SERIAL pseudo type, that’s correct. However, the mapping that Petar gives will result in the following DDL generated by Hibernate 5.1:

CREATE SEQUENCE users_id_seq START 1 INCREMENT 50;

CREATE TABLE … (
    id INT8 NOT NULL,
    …
);

This is not using SERIAL, but a Hibernate managed sequence. It is not owned by the table and no default value has been set. Of course, DDL generation is a feature that many people do not use in production (but many take the generated code as a template).

If you hand-write your DDL and actually used SERIAL, then using GenerationType.SEQUENCE may even conflict with the database behaviour. The correct way to map Hibernate with Postgres’ preferred ID strategy is using GenerationType.IDENTITY. Incidentally, the code is also much shorter and more readable:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
Sign up to request clarification or add additional context in comments.

1 Comment

what database behaviour will conflict when using GenerationType.SEQUENC
26

In PostgreSQL auto-increment is handled using the SERIAL pseudo type. You use this type when you execute CREATE TABLE.

Now to the point - this SERIAL pseudo type creates a sequence. Autoincrement in PostgreSQL is handled using the created sequence. The default value of the id column becomes - nextval('your_sequence_name').

In Hibernate for an User entity:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_seq_gen")
@SequenceGenerator(name = "users_seq_gen", sequenceName = "users_id_seq")
public Long getId() {
     return id;
}

Read here:

http://www.postgresql.org/docs/8.4/static/datatype-numeric.html#DATATYPE-SERIAL

http://www.neilconway.org/docs/sequences/

4 Comments

is there any way to have sequence that allows to have the same id through other tables? I mean User ID = 1 and Group ID = 1 ?
Every id column of every table should have a separate sequence. Look at my edit.
I guess that generating the sequence in this case will not actually be handled by Postgres, but the Hibernate instead. In the database schema there will not be default value assigned with nextval from a Postgres sequence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.