GeneratedValue in Postgres

后端 未结 2 1010
死守一世寂寞
死守一世寂寞 2020-12-10 16:22

I have my entity class mapped like below:

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

    @Id
    @GeneratedValue
    @Getter @S         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 17:03

    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/

提交回复
热议问题