Search code examples
mysqljdbcspring-data-jpahibernate-mappingsqlexception

How to solve Hibernate "ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Duplicate entry " Exception


It's been 2 days now that I test to solve this problem but nothing works. I have these entities implyed in a bidirectional relation.

@Entity
@Table( name = "T_user" )
public class T_user implements Serializable {
   @Id
   @GeneratedValue( strategy = GenerationType.IDENTITY )
   private Long               id_user;

   @OneToOne( targetEntity = T_infosProfile.class, mappedBy = "user", fetch = FetchType.LAZY, orphanRemoval = true )
   private T_infosProfile     infosProfile;

   public void setInfosProfile( T_infosProfile infosProfile ) {
    this.infosProfile = infosProfile;
   }
}

And

@Entity
@Table( name = "T_infosProfile" )
public class T_infosProfile {

   @Id
   @GeneratedValue( strategy = GenerationType.IDENTITY )
   private Long       id_infos;

   @OneToOne( targetEntity = T_user.class, fetch = FetchType.LAZY )
   @JoinColumn( name = "id_user", unique = true, nullable = false )
   private T_user user;

     @Column( name = "pourcentage_completion", length = 3, updatable = true, unique = false )
     private Integer    pourcentageCompletion;

     @Column( name = "infos_identite", nullable = false, updatable = true, length = 1, columnDefinition = "TINYINT(1)" )
     private Boolean    infosIdentiteCompleted;

     @Column( name = "infos_coordonnees", nullable = false, updatable = true, length = 1, columnDefinition = "TINYINT(1)" )
     private Boolean    infosCoordonneesCompleted;

     @Column( name = "infos_bancaires", nullable = false, updatable = true, length = 1, columnDefinition = "TINYINT(1)" )
     private Boolean    infosBancaireCompleted;

     @Column( name = "infos_chicowa", nullable = false, updatable = true, length = 1, columnDefinition = "TINYINT(1)" )
     private Boolean    infosCompleted;

   //the setter method
   public void setUser( T_user user) {
      this.user = user;
      this.infosIdentiteCompleted = true;
      this.pourcentageCompletion = 0;
      this.infosCoordonneesCompleted = this.user.getInfosCoordonnees() == null ? false : true;
      this.infosBancaireCompleted = this.user.getInfosBancaire() == null ? false : true;
      this.infosCompleted = this.user.getInfosCompleted() == null ? false : true;

      if ( this.infosCoordonneesCompleted == true ) {
        this.pourcentageCompletion = 50;
    }
      if ( this.infosBancaireCompleted == true && this.pourcentageCompletion == 50 ) {
        this.pourcentageCompletion = 75;
    }
      if ( this.infosChicowaCompleted == true && this.pourcentageCompletion == 75 ) {
        this.pourcentageCompletion = 100;
    }
  }
}

I also have this method which is used to update the user's infosProfile :

@Service
public class UIServiceImpl implements UIService {

  @Autowired
  private TinfosProfileRepository   infosProfileRepository;

  @Override
  public T_infosProfile saveInfosPorfile( T_user connectedUser ){
    T_infosProfile infosProfile = connectedUser.getInfosProfile();
    infosProfile.setUser( connectedUser );
    connectedUser.setInfosProfile( infosProfile );
    try {
        return infosProfileRepository.save( infosProfile );
    } catch ( Exception e ) {
        throw new ExceptionsDAO( e.getMessage() );
    }
}

But when I call this method it works for a one situation but for another one I got this exception :

10-04-2017 16:27:43 [http-nio-8080-exec-3] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1062, SQLState: 23000
10-04-2017 16:27:43 [http-nio-8080-exec-3] ERROR  o.h.e.jdbc.spi.SqlExceptionHelper - Duplicate entry '8' for key 'UK_bkariaocmnn2rlh477hvoqkyf'

I just want to update infosProfile's attributes of the current user. The situation where it works fine it's when only one infosProfile's attribute is updated but when I update 2 attributes at the same time and try to saveInfosProfile(), the exception is raised.

Any help please.


Solution

  • Well, after losting 4 days I found a solution which seems working. Here my solution :

    @Override
    public T_infosProfile saveInfosPorfile( T_user connectedUser ){
      T_infosProfile infosProfile = infosProfileRepository.findByUser(connectedUser); // I had to load by the user before saving it.
      infosProfile.setUser( connectedUser );
      connectedUser.setInfosProfile( infosProfile );
      try {
        return infosProfileRepository.save( infosProfile );
      } catch ( Exception e ) {
        throw new ExceptionsDAO( e.getMessage() );
      }
    }
    

    For me it a strange behavior as the user is the same as whom is saved in the table. Thanks Hibernate !