0

이 문제를 해결하기 위해 테스트했지만 2 일이 지나면 아무것도 작동하지 않습니다. 나는이 엔티티들을 양방향 관계로 암시했다.Hibernate "ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Duplicate entry"해결 방법 예외

@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()); 
    } 
} 

하지만이 메소드를 호출 할 때 한 상황에 있지만 작동 : 나는 또한 사용자의 infosProfile 업데이트하는 데 사용되는이 방법이

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

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

그리고

또 다른 하나는 예외가 있습니다 :

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' 

현재 사용자의 infosProfile 속성 만 업데이트하려고합니다. 하나의 infosProfile 속성 만 업데이트되었지만 동시에 2 개의 속성을 업데이트하고 saveInfosProfile()을 시도하면 예외가 발생합니다.

도움주세요.

답변

0

글쎄, 4 일을 잃은 후에 나는 효과가있는 해결책을 발견했다. 여기 내 솔루션 : 나 그것을 사용자로 이상한 행동에 대한

@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()); 
    } 
} 

누구를하는 것은 테이블에 저장되는 것과 동일합니다. 고마워요 최대 절전 모드!

0

오류 메시지를 살펴보면 그 열에 값 8이 이미 있음을 알기 때문에 자체적으로 설명이됩니다. 당신의 제약 조건 이름 UK_bkariaocmnn2rlh477hvoqkyf 볼 : 당신이 특정 열 추가

Duplicate entry '8' for key 'UK_bkariaocmnn2rlh477hvoqkyf' 

UNIQUE CONSTRAINT을 정의한 이후 그 오류를 던지고. 그것은 당신이 제약 조건을 생성하는 동안 이름을 지정하지 않을 때 발생합니다.

+0

내가 알고 있지만 난 그냥 기존 개체를 저장 볼 수 있듯이 나는이 예외가 발생 왜 내가 모르는 새로운 인스턴스를 생성하지 않는

. – akuma8

+0

당신은 무엇을 의미합니까? "당신이 제약 조건을 생성하는 동안 이름을 지정하지 않으면 그 일이 일어납니다." – akuma8

+0

@ akuma8, 시스템 생성시 읽을 수없는 제약 조건 이름이 나타납니다. – Rahul

0

내 봄 프로젝트에서 잠시 동안이 문제를 겪었으므로 아래 코드를 사용하여 내가 직면 한 문제를 해결할 수있었습니다.

infosProfile=null; 

    try { 

     infosProfile = infosProfileRepository.save(infosProfile); 
    } 
    catch (PersistenceException ex) { 

     Throwable cause1 = ex.getCause(); 

     if(!isNull(cause1)){ 
      throw new UnsupportedOperationException(cause1.getCause().getMessage()); 

     } 
     throw new UnsupportedOperationException(ex.getMessage()); 
    }