JPA: Duplicate identifier in table for: a field trying to do an update

I am trying to execute an update using jpa in a Spring-Boot app. Is very strange because I see on the console that an update is being run but after it runs the console shows

: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Duplicate identifier in table for:

I dont understand I dont see the insert statement.

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "porta")
public class ProfileEntity {
    @Id
    @Column(name = "idporta")
    private Long id;
    @Column(name = "title")
    private String name;
    @Column(name = "imageURL")
    private String image;
    @Column(name = "description")
    private String description;
    @Column(name = "twitterName")
    private String twitterName;

}

On the config:

spring:
  jpa:
    database-platform: org.hibernate.dialect.MySQL5Dialect
    hibernate:
      ddl-auto: update
      naming:
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    properties:
      hibernate: {show_sql: true, use_sql_comments: true, format_sql: true}

Also working with the next jpa libs

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

And the log is:

Hibernate: 
    /* update
        com.app.sample.entities.ProfileEntity */ update
            portfo
        set
            description=?,
            imageURL=?,
            title=?,
            twitterUse=? 
        where
            idporta=?
2019-11-14 00:59:06.524 ERROR 21032 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Duplicate identifier in table for: [com.app.portafolioweb.entities.ProfileEntity#0]; nested exception is org.hibernate.HibernateException: Duplicate identifier in table for: [com.app.sample..entities.ProfileEntity#0]] with root cause

org.hibernate.HibernateException: Duplicate identifier in table for: [com.app.sample..entities.ProfileEntitys#0]
    at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2563) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3391) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3265) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3666) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:149) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]

1 answer

  • answered 2019-11-14 05:38 Gaurav Dhiman

    Is there any trigger on the table that updates the same table. In a case where a FOR UPDATE trigger is fired any update statement would return 2 (or more) updated rows, making the Check think that a duplicated identifier exists.