2017-04-10 102 views
0

我已經過了2天的測試來解決這個問題,但沒有任何工作。 我有這些實體隱含在雙向關係中。如何解決Hibernate「ERROR o.h.e.jdbc.spi.SqlExceptionHelper - 重複項」異常

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

而且

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

我也有這樣的方法,該方法被用來更新用戶的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()); 
    } 
} 

但是,當我把這種方法,它適用於一個情況,但對於另一個我得到這個例外:

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已經存在於該列中。它拋出這個錯誤,因爲你已經在那個特定的列

Duplicate entry '8' for key 'UK_bkariaocmnn2rlh477hvoqkyf' 

其他定義的UNIQUE CONSTRAINT:看看你的約束名UK_bkariaocmnn2rlh477hvoqkyf。如果您在創建約束時未指定約束,那會發生這種情況。

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