gpt4 book ai didi

java - 方法返回后尝试事务提交时抛出 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 09:12:45 25 4
gpt4 key购买 nike

在保存我的实体时,我遇到了一个非常奇怪的 NullPointerException,但我似乎不明白为什么。

java.lang.NullPointerException: null
at org.hibernate.type.AbstractType.getHashCode(AbstractType.java:129)
at java.base/java.util.stream.Collectors.lambda$groupingBy$53(Collectors.java:1127)
at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at org.hibernate.collection.internal.PersistentBag.groupByEqualityHash(PersistentBag.java:196)
at org.hibernate.collection.internal.PersistentBag.equalsSnapshot(PersistentBag.java:151)
at org.hibernate.engine.spi.CollectionEntry.dirty(CollectionEntry.java:158)
at org.hibernate.engine.spi.CollectionEntry.preFlush(CollectionEntry.java:182)
at org.hibernate.event.internal.AbstractFlushingEventListener.lambda$prepareCollectionFlushes$0(AbstractFlushingEventListener.java:195)
at org.hibernate.engine.internal.StatefulPersistenceContext.forEachCollectionEntry(StatefulPersistenceContext.java:1091)
at org.hibernate.event.internal.AbstractFlushingEventListener.prepareCollectionFlushes(AbstractFlushingEventListener.java:194)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:86)
at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:50)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108)
at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1323)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1403)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1558)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1526)
at org.hibernate.query.Query.getResultList(Query.java:165)
[...] the rest was cut for readability (if you think it's really important, I'll edit and add the whole exception, but I think it's the first line that says it all)

这是由于上述异常而引发的较高级异常:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:542)

还有这个嵌套的:

Caused by: javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.internal.ExceptionConverterImpl.convertCommitException

我一整天都在兜圈子,试图找出为什么会发生这种情况,但互联网没有帮助,因为大多数类似的问题仍然没有答案。更糟糕的是 Spring 的抽象使得调试变得不可能!

场景如下:我有一个正在尝试更新的 EventEntity 及其子实体 EventAttachmentEntity。我已经打开日志记录来跟踪正在发生的事情,并且几乎发生了预期的情况:

  • 我已更新 eventEntity(更新生成的 SQL)
  • 我已为更新的事件插入了新附件(插入使用正确的 event_id 生成的 SQL)

然后奇怪的事情发生了,我的方法到达末尾,然后发生了这个异常。

出于公开原因,对该方法进行了抽象,但主要流程如下:

@Transactional(rollbackFor = Exception.class)
public void updateEvent(EventEntity event) {
EventEntity updatedEvent = eventRepository.save(event);

EventAttachmentEntity newAttachment = new EventAttachmentEntity(updatedEvent);
// fill attachment [...]
newAttachment.setEvent(updatedEvent);
eventAttachmentRepository.save(newAttachment);

System.out.println("This line gets printed.");
}

所以首先我保存更新的父级,然后创建新的子级,设置父级,然后保存它。所有 SQL 都证实这就是所发生的情况。最后一行也会打印出来,这意味着以前的保存方法没有任何问题。然后它到达了终点,轰隆隆!异常。

这是我的实体:

@Entity
@Table(name = "event", schema = "xxx")
@DynamicUpdate
public class EventEntity {
private long id;
private Collection<EventAttachmentEntity> eventAttachments;

// other properties abstracted

@OneToMany(mappedBy = "event")
public Collection<EventAttachmentEntity> getEventAttachments() {
return this.eventAttachments;
}

public void setEventAttachments(Collection<EventAttachmentEntity> eventAttachments) {
this.eventAttachments = eventAttachments;
}
}
@Entity
@Table(name = "event_attachment", schema = "xxx")
public class EventAttachmentEntity {
private long id;
private String filename;
private String contentType;
private long filesize;
private EventEntity event;

// other properties abstracted

@ManyToOne
@JoinColumn(name = "event_id", referencedColumnName = "id", nullable = false)
public EventEntity getEvent() {
return event;
}

public void setEvent(EventEntity event) {
this.event = event;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EventAttachmentEntity that = (EventAttachmentEntity) o;
return id == that.id &&
filesize == that.filesize &&
Objects.equals(filename, that.filename) &&
Objects.equals(contentType, that.contentType);
}

@Override
public int hashCode() {
return Objects.hash(id, filename, contentType, filesize);
}
}

我为 EventAttachmentEntity 添加了 equals()hashCode() 方法,因为异常显然发生在 getHashCode() 处,但当我在那里放置断点时,实际上调用 getHashCode 的对象为 null。

重要说明:我已经检查了代码中的任何变量是否为空或尚未正确自动连接 - 一切都很好。正如我所说,该方法本身运行平稳,直到到达末尾并尝试提交事务。这就是它崩溃的时候。

此外,当我尝试仅更新事件(没有附件)时,一切正常。我尝试过的另一个案例是保存此事件的类似关系(此处未显示),它也工作得很好。所以这与附件有关,但我不知道它是什么!

同样奇怪的是,如果我从此方法中删除@Transactional,它会抛出相同的异常。没有 @Transactional 标记的方法调用 updateEvent(),所以我不确定这是如何发生的。

拜托,我需要任何关于可能出现问题的建议,以便我可以尝试一下,因为现在我几乎碰壁了,无法继续。

最佳答案

您没有显示完整的代码,我们只能猜测您那里有什么。根据显示的代码,成员变量EventEntity.eventAttachments未初始化。当您尝试保留此类实体时,这可能会导致 NPE。

解决方案

初始化它,例如如下:

private Collection<EventAttachmentEntity> eventAttachments = new HashSet<>();

此外,在 setEventAttachments 中,确保无论将什么参数传递给此 setter ,eventAttachment 都保持不为 null。

关于java - 方法返回后尝试事务提交时抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com