gpt4 book ai didi

Hibernate - 重复键值违反唯一约束

转载 作者:行者123 更新时间:2023-11-29 12:28:09 26 4
gpt4 key购买 nike

我有以下问题

@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;

...

假设 Pessoa 表有 id 1 到 500 的记录。当我运行下面的代码时:

PessoaFisica pessoaFisica = new PessoaFisica();
pessoaFisica.setEmail("teste@123.com");

...

em.persist(pessoa);

出现以下错误:

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.

为什么要尝试使用 ID 438 而不是 501???

有人知道会发生什么吗?

非常感谢

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;

记录 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.

最佳答案

如果您正在持久化新实体,请尝试将 ID 设置为 null

PessoaFisica pessoaFisica = new PessoaFisica();
pessoaFisica.setID(null);
pessoaFisica.setEmail("teste@123.com");

...

em.persist(pessoa);

这将确保当 hibernate 试图持久化对象时该对象是新的。

关于Hibernate - 重复键值违反唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com