Hibernate id com auto_increment

Galera estou com uma dúvida, eu tenho duas tabelas: Contato e Curso
estou utilizando o hibernate 5.2, eu coloquei para hibernate criar os id desta maneira:
Tabela Contato:

@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "contato_id")
    public Integer getId() {
        return this.id.get();
    }

Tabela Curso:

@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "curso_id")
    public int getId(){
        return this.id.get();
    }

Blz até ai tudo bem, mais ao adicionar dois cursos ele faz o id incrementar em sequência. Mas ao adicionar um Contato ele já começa com o ID 3
Ou seja são de tabelas diferentes, mas o contato adicionou a partir do id 3, alguém poderia me explicar?
Ex:
Tabela Curso:
Id--------------nome
1--------------Cabeleireiro
2--------------Web Designer

Tabela Contato
Id--------------nome
3--------------João
4--------------Maria

Se quiserem olhas minhas entidades aqui vai:

@Entity
@Table(name = "cursos")
public class Curso implements Serializable {
    private final IntegerProperty id;
    private final StringProperty nome;
    private final IntegerProperty carga_horaria;

    public Curso() {
        this.id = new SimpleIntegerProperty();
        this.nome = new SimpleStringProperty();
        this.carga_horaria = new SimpleIntegerProperty();
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "curso_id")
    public int getId(){
        return this.id.get();
    }
    
    @Column(name = "nome", length = 60)
    public String getNome(){
        return this.nome.get();
    }
    @Column(name = "carga_horaria")
    public int getCarga(){
        return this.carga_horaria.get();
    }
    
    protected void setId(int id){
        this.id.set(id);
    }
    
    public void setNome(String nome){
        this.nome.set(nome);
    }
    
    public void setCarga(int carga){
        this.carga_horaria.set(carga);
    }
    
    public IntegerProperty idProperty(){
        return this.id;
    }
    
    public IntegerProperty cargaProperty(){
        return this.carga_horaria;
    }
    
    public StringProperty nomeProperty(){
        return this.nome;
    }
}

Minha outra entidade.

@Entity
@Table(name = "contatos")
public class Contato implements Serializable {

    private final IntegerProperty id;
    private final StringProperty nome;
    private final StringProperty telefone;
    private StringProperty observacao;
    private LocalDate data_retorno;
    private Curso curso_pretendido;
    private Situacao situacao;
    
    public Contato() {
        this.id = new SimpleIntegerProperty();
        this.nome = new SimpleStringProperty();
        this.telefone = new SimpleStringProperty();
        this.observacao = new SimpleStringProperty();
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "contato_id")
    public Integer getId() {
        return this.id.get();
    }
    @Column(name = "nome", length = 40, nullable = false)
    public String getNome() {
        return nome.get();
    }
    @Column(name = "telefone", length = 20)
    public String getTelefone() {
        return telefone.get();
    }
    @Column(name = "observacao", columnDefinition = "text")
    public String getObservacao() {
        return observacao.get();
    }
    
    @Column(name = "data_retorno")
    public LocalDate getDataRetorno() {
        return data_retorno;
    }
    @OneToOne()
    @JoinColumn(name = "curso_id")
    public Curso getCursoPretendido() {
        return curso_pretendido;
    }
    @Enumerated(EnumType.STRING)
    @Column(name = "situacao")
    public Situacao getSituacao() {
        return situacao;
    }
    
    public IntegerProperty idProperty() {
        return id;
    }
    
    public StringProperty nomeProperty() {
        return nome;
    }
    
    public StringProperty telefoneProperty() {
        return telefone;
    }
    
    public StringProperty observacaoProperty() {
        return observacao;
    }
    
    protected void setId(int id) {
        this.id.set(id);
    }

    public void setNome(String nome) {
        this.nome.set(nome);
    }

    public void setTelefone(String telefone) {
        this.telefone.set(telefone);
    }

    public void setObservacao(String observacao) {
        this.observacao.set(observacao);
    }
    
    public void setCursoPretendido(Curso curso){
        this.curso_pretendido = curso;
    }
    
    public void setDataRetorno(LocalDate data){
        this.data_retorno = data;
    }

    public void setSituacao(Situacao situacao) {
        this.situacao = situacao;
    }
    
    
}

Bom dia
se você estiver usando o MySQL o auto increment é feito assim

@GeneratedValue(strategy= GenerationType.IDENTITY)

espero ter ajudado

1 curtida

Obrigado mano, era isssooo mesmo valeu jovem :slight_smile:

Boa noite. Estou com o mesmo problema, só que estou usando o PostgreSQL. Tentei trocar pra identity, sequence e depois auto, mas continuou dando errado. O que pode ser?