gpt4 book ai didi

java - Spring、Hibernate : Database call complete for saving object, 但有时返回 null 值

转载 作者:行者123 更新时间:2023-12-01 09:19:54 25 4
gpt4 key购买 nike

我正在使用 Hibernate 和 PostgreSQL 开发 Spring-MVC 应用程序,这些天我们遇到了一个非常奇怪的问题。有时,当一个对象被持久化时,我会将它的 ID 从 DAO 层返回到服务层。然后通过我们的 PUSH 框架广播该 ID,之后通过调用检索该对象。此时我得到一个 NPE,虽然对象已保存,但主键 ID 不为零, session 在返回 ID 之前也被刷新。可能出了什么问题?

示例:

@Override
public void addNestedGroupNoteComment(String commentText, int noteId, int commentId){
int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory, noteId);

if(saveId!=0) {
notification.setCommentId(saveId);
this.chatService.sendNotification(notification, groupMembers.getMemberid());
}

DAO 方法:

@Repository
@Transactional
public class GroupNoteHistoryDAOImpl implements GroupNoteHistoryDAO {

private final SessionFactory sessionFactory;


@Autowired
public GroupNoteHistoryDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public int addGroupNoteComment(GroupNoteHistory noteHistory, int noteId) {
Session session = this.sessionFactory.getCurrentSession();
GroupNotes notes = (GroupNotes) session.get(GroupNotes.class, noteId);
if(notes!=null) {
notes.getGroupNoteHistorySet().add(noteHistory);
noteHistory.setMhistory(notes);
int saveId = (Integer) session.save(noteHistory);
session.flush();
return saveId;
}
}
}

现在,在广播 ID 后,调用此方法:

@Override
public GroupNoteHistory getGroupNoteHistoryById(int historyId) {
GroupNoteHistory groupNoteHistory = this.groupNoteHistoryDAO.getGroupNoteHistoryById(historyId);
// Above object is many times null, tried System.out, it was with non-zero values.
}

DAO 方法:

@Override
public GroupNoteHistory getGroupNoteHistoryById(int historyId) {
Session session = this.sessionFactory.getCurrentSession();
return (GroupNoteHistory) session.get(GroupNoteHistory.class, historyId);
}

有什么想法吗?

更新

错误日志:

HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

type Exception report

message Request processing failed; nested exception is java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

root cause

java.lang.NullPointerException
com.project_name.spring.service.GroupNoteHistoryServiceImpl.getGroupNoteHistoryById(GroupNoteHistoryServiceImpl.java:156)
sun.reflect.GeneratedMethodAccessor647.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)

最佳答案

这里int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory, noteId);您已经有了一个GroupeNoteHistory。

在此之后,您可以操作它并保存它

        noteHistory.setMhistory(notes);
int saveId = (Integer) session.save(noteHistory);
session.flush();
return saveId;`

所以在使用之前必须检查GroupNoteHistory是否已经存在并且不为空。这就是您获得 NPE 的原因。

关于java - Spring、Hibernate : Database call complete for saving object, 但有时返回 null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40235130/

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