Hibernate-重复键值违反唯一约束
我有以下问题Hibernate-重复键值违反唯一约束,hibernate,postgresql,jpa,Hibernate,Postgresql,Jpa,我有以下问题 @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
@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;
...
假设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.
日志休眠:
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尝试持久化对象时该对象是新的。如果要持久化新实体,那么尝试将
ID
设置为null
怎么样
PessoaFisica pessoaFisica = new PessoaFisica();
pessoaFisica.setID(null);
pessoaFisica.setEmail("teste@123.com");
...
em.persist(pessoa);
这将确保在hibernate尝试持久化对象时该对象是新的。是否有特定的原因导致您没有使用
串行文件
,并告诉hibernate该ID是自动生成的?在持久化之前,您的序列显示了什么?它可能不是从500开始的。您应该从数据库中检查它吗?现在添加hibernate日志。。。tks.resolved为allocationSize=1
。。。tk您没有使用串行
并告诉Hibernate ID是自动生成的,有什么具体原因吗?在持久化之前,您的序列显示了什么?它可能不是从500开始的。您应该从数据库中检查它吗?现在添加hibernate日志。。。tks.resolved为allocationSize=1
。。。它已经是新的了。它没有得到管理。将null设置为ID的目的是什么?如果将null
或0
零值设置为ID,则hibernate将其假定为新对象。我已经知道了。因为它是一个新对象(new-peasoafisica();
),所以默认情况下它的null
。如果反复将null
分配给ID
,则不会更改任何内容。尚未更改任何内容..:(…TksIt已经是新的。它没有被管理。将null设置为ID的目的是什么?如果将null
或0
零值设置为ID,则hibernate将其假定为一个新对象。我已经知道了。因为它是一个新对象(new peasoafisca();
)默认情况下,它的null
。如果反复将null
分配给ID
,则不会有任何更改。还没有任何内容:(…Tks
PessoaFisica pessoaFisica = new PessoaFisica();
pessoaFisica.setID(null);
pessoaFisica.setEmail("teste@123.com");
...
em.persist(pessoa);