如何解决Hibernate "ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Duplicate entry "异常
How to solve Hibernate "ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Duplicate entry " Exception
我测试解决这个问题已经2天了,但没有任何效果。
我在双向关系中暗示了这些实体。
@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() 时,会引发异常。
请帮忙。
查看错误消息,它不言自明,因为它说值 8
已经存在于该列中。它抛出该错误,因为您在该特定列
上定义了 UNIQUE CONSTRAINT
Duplicate entry '8' for key 'UK_bkariaocmnn2rlh477hvoqkyf'
附加:查看您的约束名称UK_bkariaocmnn2rlh477hvoqkyf
。如果您在创建约束时没有命名约束,就会发生这种情况。
好吧,在浪费了 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() );
}
}
对我来说这是一个奇怪的行为,因为用户与 table 中保存的用户相同。感谢休眠!
在我的 spring 项目中解决这个问题一段时间后,我能够使用下面的代码解决我面临的问题。
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());
}
我测试解决这个问题已经2天了,但没有任何效果。 我在双向关系中暗示了这些实体。
@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() 时,会引发异常。
请帮忙。
查看错误消息,它不言自明,因为它说值 8
已经存在于该列中。它抛出该错误,因为您在该特定列
UNIQUE CONSTRAINT
Duplicate entry '8' for key 'UK_bkariaocmnn2rlh477hvoqkyf'
附加:查看您的约束名称UK_bkariaocmnn2rlh477hvoqkyf
。如果您在创建约束时没有命名约束,就会发生这种情况。
好吧,在浪费了 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() );
}
}
对我来说这是一个奇怪的行为,因为用户与 table 中保存的用户相同。感谢休眠!
在我的 spring 项目中解决这个问题一段时间后,我能够使用下面的代码解决我面临的问题。
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());
}