Generation Strategy Id Hibernate in PostgreSQL

3

I have a system in JEE7 with Hibernate and PostgreSQL database, the tables are with auto Id generation by Hibernate:

@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

What happens is that Hibernate generates a unique index for all tables, for example, an id = 657 for Client and the next id = 658 for Product, .....

How would it be possible to generate an index automatically for each table?

Thank you.

    
asked by anonymous 18.05.2016 / 06:00

2 answers

4

Alternatively it is also possible to use a column of type SERIAL combined with a IDENTITY generation strategy on the Hibernate side.

@Id
@Column(name = "meu_id", columnDefinition = "serial")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
    
18.05.2016 / 15:38
2

I think the best way is to create the sequence for each table in the database and then set the model to use it:

To create the sequence, use:

CREATE SEQUENCE seq_empresa START 1;

To tell hibernate to use this sequence:

@Id
@Column(name = "id_empresa")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_empresa")
private Long idEmpresa;
    
18.05.2016 / 14:27