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

标签 java spring postgresql hibernate spring-mvc

我正在使用 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/

相关文章:

java - 访问另一个类的公共(public)方法时出现问题

java - Spring-Boot 多个数据库目标依赖项

postgresql - Postgresql select 命令中的 isNullOrEmpty 相当于什么?

java - 在selenium处理程序java中单击事件后获取旧的html

java - 在数组中除最后一项之外的项之间插入分号

java - Java 原始包装器的泛型

java - Spring 以合格的 bean 为条件

spring - ResponseEntity<JSON> - header 中没有内容长度

sql - 将函数移动到另一个模式

postgresql - Postgres : How to verify which certificate is being used for SSL