GeneratedValue in Postgres

I have an entity class as shown below:

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

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

I am creating a clean new database and properties:

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

WHY ADA (Sorry, almost two days spent on this error) after creating the database, I got the 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 do not want to have a sequence, I want to have only an automatic increase in automatically generated values.

+3
source share
2 answers

PostgreSQL auto-increment handles a type alias SERIAL. This type is used at runtime CREATE TABLE.

- - SERIAL . PostgreSQL . id - nextval('your_sequence_name').

Hibernate User:

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

:

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

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

+15

, . Postgres SERIAL, . , Petar, DDL, Hibernate 5.1:

CREATE SEQUENCE users_id_seq START 1 INCREMENT 50;

CREATE TABLE … (
    id INT8 NOT NULL,
);

SERIAL, Hibernate. , . , DDL - , ( ).

DDL SERIAL, GenerationType.SEQUENCE . Hibernate Postgres GenerationType.IDENTITY. , :

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
+9

All Articles