2017-04-10 11 views
0

この問題を解決するためにテストが行​​われていますが、何も機能していません。 私は、これらのエンティティを双方向関係に含めるようにしました。Hibernateの解決方法 "ERROR o.h.e.jdbc.spi.SqlExceptionHelper - 重複したエントリ"例外

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

もう1つ例外があります:

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の属性が1つしか更新されていないのに、同時に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()); 
    } 
} 

それはユーザーとしての奇妙な行動は、テーブルに保存されている人と同じです。ありがとうHibernate!

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

関連する問題