Spring数据jpa';don';t在异常后刷新会话';从4.3迁移到hibernate 5.x后发生

Spring数据jpa';don';t在异常后刷新会话';从4.3迁移到hibernate 5.x后发生,hibernate,spring-boot,spring-data-jpa,Hibernate,Spring Boot,Spring Data Jpa,我正在使用SpringBoot和SpringDataJPA,我正在持久化具有自动生成的实体和一个唯一字段,如下所示 @Entity @Table(name="ENTITY") public class Entity { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long entityId; @Column(name="data",unique=true) private Long da

我正在使用SpringBoot和SpringDataJPA,我正在持久化具有自动生成的实体和一个唯一字段,如下所示

@Entity
@Table(name="ENTITY")
public class Entity {

    @Id @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long entityId;

    @Column(name="data",unique=true)
    private Long data;
}
try{

//Save operation with null entityId
saveEntity(entity);
}catch(DataIntegrityViolationException)
{
   //Here I am fetching other entity 
}
所以,在使用空entityId保存实体(因为它是自动生成的)和重复的数据字段时,它会

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'DL783E6D8D' for key 'UK_htwi98cnnh794ddgu2dic3nm1'
这是用try-catch块处理的,如下所示

@Entity
@Table(name="ENTITY")
public class Entity {

    @Id @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long entityId;

    @Column(name="data",unique=true)
    private Long data;
}
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)

Hibernate一直记录着异常不可恢复的事实,并且在异常发生后唯一安全的做法是回滚事务并关闭会话,这将无法再使用。如果它以前起作用,那就是一场意外。在会话引发异常后,不要尝试重用该会话。@jbnize很抱歉,我错过了事务并回滚。.我已更新了问题如果您从另一个事务方法调用saveEntity方法,则所有代码仍在同一事务中执行,使用相同的会话。Thank@JBNizet从调用saveEntity的方法中删除了事务。这就解决了问题。请在回答中添加您的上述评论,我将接受。Hibernate始终记录了异常不可恢复的事实,并且在异常发生后唯一安全的做法是回滚事务并关闭会话,这将无法再使用。如果它以前起作用,那就是一场意外。在会话引发异常后,不要尝试重用该会话。@jbnize很抱歉,我错过了事务并回滚。.我已更新了问题如果您从另一个事务方法调用saveEntity方法,则所有代码仍在同一事务中执行,使用相同的会话。Thank@JBNizet从调用saveEntity的方法中删除了事务。这就解决了问题。请在回答中添加您的上述评论,我将接受。