1

I Need to insert one record in my DB, but in some cases I need to pass the ID but when I insert my id manually the spring-boot don`t save with this ID, the spring-boot got a new from sequence

this is my Entity

 @Entity
@Table(schema = "db_cartorio_inteligente",  name = "tb_movimentacao")
@SequenceGenerator(sequenceName="db_cartorio_inteligente.tb_movimentacao_seq", name = "tb_movimentacao_seq")
public class TbMovimentacao {
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "tb_movimentacao_seq")
    private Long idMovimentacao;

and this is my code to save:

TbMovimentacao novaMovimentacao = new TbMovimentacao();
novaMovimentacao.setIdMovimentacao(12323);
this.movimentacaoRepository.save(novaMovimentacao);

instead save with id 12323 he got the next from my sequence

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Fabio Ebner
  • 2,613
  • 16
  • 50
  • 77
  • I think you might have to create your own `Id` generator for this, check [this](http://stackoverflow.com/q/3194721/3274974) question out, it has some solutions for your use case. – Edd Jan 20 '17 at 07:05
  • For your reference: http://stackoverflow.com/questions/31158509/how-to-generate-custom-id-using-hibernate-while-it-must-be-primary-key-of-table. Since persistence provider assigns the primary key before the insertion it overrides the value set before – Barath Jan 20 '17 at 07:08

1 Answers1

1

You defined it to use a generator, so it will generate the id as you asked it to. You setting it first is of no relevance. Remove the generator if you want to set it

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29