2

I have the following problem

@Entity
@Table(name="tb_pessoa", schema="public")
@Inheritance(strategy = InheritanceType.JOINED)
public class Pessoa implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @SequenceGenerator(name = "tb_pessoa_id_seq", sequenceName = "tb_pessoa_id_seq", schema="public")
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "tb_pessoa_id_seq")
   @Column(name = "id", nullable = false)
   private Integer id;

   ...

@Entity  
@Table(name="tb_pessoafisica", schema="public")
@PessoaFisicaAnnotation
@PrimaryKeyJoinColumn(name = "id")
public class PessoaFisica extends Pessoa implements Serializable {
   private static final long serialVersionUID = 1L;

   @Column(name = "email")
   private String email;

   ...

Assuming the Pessoa table has records with id 1 to 500. When I run the code below:

PessoaFisica pessoaFisica = new PessoaFisica();
pessoaFisica.setEmail("[email protected]");

...

em.persist(pessoa);

It's happening the following error:

17:26:13,613 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--10.36.1.49-8180-1) ERROR: duplicate key value violates unique constraint "tb_pessoa_pkey"
    Detalhe: Key (id)=(438) already exists.
17:26:13,614 WARN  [com.arjuna.ats.arjuna] (http--10.36.1.49-8180-1) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffff7f000101:-7f6ae4e3:558080ed:4f, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@331af73a >: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: ERROR: duplicate key value violates unique constraint "tb_pessoa_pkey"
  Detalhe: Key (id)=(438) already exists.

why is trying to use the id 438 and not 501???

Does anyone know what might be happening?

thank you very much

CREATE TABLE tb_pessoa
(
    id integer NOT NULL,
    CONSTRAINT tb_pessoa_pkey PRIMARY KEY (id)
)

CREATE TABLE tb_pessoafisica
(
    email character varying(64) NOT NULL,
    CONSTRAINT tb_pessoafisica_pkey PRIMARY KEY (id),
    CONSTRAINT fkee8c050f70415778 FOREIGN KEY (id)
        REFERENCES tb_pessoa (id) MATCH SIMPLE
)

CREATE SEQUENCE tb_pessoa_id_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 500
  CACHE 1;

Log Hibernate:

07:50:52,463 INFO  [stdout] (http--10.36.1.49-8180-2) Hibernate: 
07:50:52,463 INFO  [stdout] (http--10.36.1.49-8180-2)     select
07:50:52,464 INFO  [stdout] (http--10.36.1.49-8180-2)         nextval ('public.tb_pessoa_id_seq')
07:50:52,497 INFO  [stdout] (http--10.36.1.49-8180-2) Hibernate: 
07:50:52,498 INFO  [stdout] (http--10.36.1.49-8180-2)     insert 
07:50:52,499 INFO  [stdout] (http--10.36.1.49-8180-2)     into
07:50:52,499 INFO  [stdout] (http--10.36.1.49-8180-2)         public.tb_pessoa
07:50:52,500 INFO  [stdout] (http--10.36.1.49-8180-2)         (id) 
07:50:52,500 INFO  [stdout] (http--10.36.1.49-8180-2)     values
07:50:52,501 INFO  [stdout] (http--10.36.1.49-8180-2)         (?)
07:50:52,519 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--10.36.1.49-8180-2) SQL Error: 0, SQLState: 23505
07:50:52,521 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--10.36.1.49-8180-2) ERROR: duplicate key value violates unique constraint "tb_pessoa_pkey"
    Detalhe: Key (id)=(438) already exists.
4
  • Is there a specific reason you didn't use a serial and tell Hibernate that the ID is auto-generated? Commented Jun 17, 2015 at 5:47
  • What is your sequence is showing before persisting? It may not start from 500. Should you check it from database? Commented Jun 17, 2015 at 6:16
  • Add the hibernate log now... tks. Commented Jun 17, 2015 at 10:57
  • resolved with allocationSize = 1 ... Tks Commented Jun 17, 2015 at 20:41

1 Answer 1

-1

If you are persisting new entity, what about trying to set ID to null.

PessoaFisica pessoaFisica = new PessoaFisica();
pessoaFisica.setID(null);
pessoaFisica.setEmail("[email protected]");

...

em.persist(pessoa);

This will ensure that the object is new when hibernate trying to persist it.

Sign up to request clarification or add additional context in comments.

4 Comments

Its already new. Its not managed. What is the purpose of setting null to ID?
If a null or 0 zero value is set to ID, then hibernate assume it as a new object.
I already know that. Since its a new object (new PeassoaFisica();) by default its null. Nothing will change if you assign null to ID again and again.
Nothing yet.. :(... Tks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.