Spring boot Spring Boot JPA EntityListener查询原因“;don';t在异常发生后刷新会话";
问题: 我使用Spring boot Spring Boot JPA EntityListener查询原因“;don';t在异常发生后刷新会话";,spring-boot,hibernate,jpa,exception,Spring Boot,Hibernate,Jpa,Exception,问题: 我使用@PostPersist方法创建对象A,该方法将创建对象B,这就像一个符咒! 在创建对象B之前,我需要介绍一些逻辑,我需要查询数据库,看看数据库中是否已经存在类似的B对象。但是当我运行我的查询时 @Query("select case when count(n) > 0 then true else false end from Notification n where student = :student and initiator = :initiator an
@PostPersist
方法创建对象A,该方法将创建对象B,这就像一个符咒!
在创建对象B之前,我需要介绍一些逻辑,我需要查询数据库,看看数据库中是否已经存在类似的B对象。但是当我运行我的查询时
@Query("select case when count(n) > 0 then true else false end from Notification n where student = :student and initiator = :initiator and entityType = :entityType and entityId = :entityId")
boolean alreadyNotified(@Param("student") Student student, @Param("initiator") Student initiator, @Param("entityType") EntityType entityType, @Param("entityId") Long entityId);
我得到以下错误:
ERROR org.hibernate.AssertionFailure.<init>(31) - HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in se.hitract.model.Likes entry (don't flush the Session after an exception occurs)
org.hibernate.AssertionFailure: null id in se.hitract.model.Likes entry (don't flush the Session after an exception occurs)
可能的听众:
@Component
public class LikeListener {
@PostPersist
public void doThis(Likes like) {
NotificationService notificationService = BeanUtil.getBean(NotificationService.class);
if(like.getEntityType().equals(EntityType.INSPIRATION)) {
InspirationService inspirationService = BeanUtil.getBean(InspirationService.class);
Inspiration inspiration = inspirationService.get(like.getEntityId());
notificationService.createLikeNotification(inspiration.getStudent(), like.getStudent(), EntityType.INSPIRATION, inspiration.getId());
}
if(like.getEntityType().equals(EntityType.COMMENT)) {
CommentService commentService = BeanUtil.getBean(CommentService.class);
Comment comment = commentService.get(like.getEntityId());
notificationService.createLikeNotification(comment.getStudent(), like.getStudent(), EntityType.COMMENT, comment.getId());
}
}
}
问题是:
public Notification createLikeNotification(Student student, Student initiator, EntityType entityType, Long entityId) {
if(student.equals(initiator) || alreadyNotified(student, initiator, entityType, entityId)) {
return null;
}
Notification notification = createNotification(student,
initiator,
NOTIFICATION_TYPE.LIKE,
entityType,
entityId,
null);
return repository.save(notification);
}
public boolean alreadyNotified(Student student, Student initiator, EntityType entityType, Long entityId) {
return repository.alreadyNotified(student, initiator, entityType, entityId);
}
如果我删除alreadyNotified
-调用,则不会引发错误。我错过了什么?
似乎Hibernate在我的查询运行之前刷新了Likes保存,但随后它失败了。我需要手动刷新/刷新吗?我认为Hibernate应该为我解决这个问题。请参阅,第3.5.2节:“一般来说,可移植应用程序的生命周期方法不应调用EntityManager或查询操作,访问其他实体实例,或修改同一持久性上下文中的关系”。
public Notification createLikeNotification(Student student, Student initiator, EntityType entityType, Long entityId) {
if(student.equals(initiator) || alreadyNotified(student, initiator, entityType, entityId)) {
return null;
}
Notification notification = createNotification(student,
initiator,
NOTIFICATION_TYPE.LIKE,
entityType,
entityId,
null);
return repository.save(notification);
}
public boolean alreadyNotified(Student student, Student initiator, EntityType entityType, Long entityId) {
return repository.alreadyNotified(student, initiator, entityType, entityId);
}