我正在使用带有弹簧数据jpa的spring boot,我是持久化实体,具有自动生成和一个唯一字段如下
@Entity
@Table(name="ENTITY")
public class Entity {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long entityId;
@Column(name="data",unique=true)
private Long data;
}
因此,在保存具有null entityId的实体(因为它是自动生成的)和重复的数据字段时,它会显示
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'DL783E6D8D' for key 'UK_htwi98cnnh794ddgu2dic3nm1'
使用try catch块处理,如下所示
try{
//Save operation with null entityId
saveEntity(entity);
}catch(DataIntegrityViolationException)
{
//Here I am fetching other entity
}
saveEntity函数使用spring @Transaction
并回滚DataIntegrityViolationException
@Transactional(rollbackFor={DataIntegrityViolationException.class})
public void saveEntity(Entity entity)
{
entityRepository.save(entity);
}
他们整个设置工作正常,直到我迁移到hibernate 5.0.9,迁移后它给出了错误
org.hibernate.AssertionFailure - HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in Entity entry (don't flush the Session after an exception occurs)