Spring,Hibernate:数据库调用完成以保存对象,但有时会返回null值

时间:2016-10-25 08:33:28

标签: java spring postgresql hibernate spring-mvc

我正在使用Hibernate和PostgreSQL进行Spring-MVC应用程序,现在我们遇到了一个非常奇怪的问题。有时当一个对象被持久化时,我将它的ID从DAO层返回到服务层。然后通过我们的PUSH框架广播该ID,之后通过调用检索该对象。此时我正在获取NPE,虽然保存了对象,但主键ID不为零,会话也会在返回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)

2 个答案:

答案 0 :(得分:1)

此处int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory, noteId);您已拥有GroupeNoteHistory。

在此之后你操纵并保存它

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

因此,在使用之前,您必须检查GroupNoteHistory是否已存在且不为null。这就是你获得NPE的原因。

答案 1 :(得分:0)

最后这对我有用:

  @Override
    public int addGroupNoteComment(GroupNoteHistory noteHistory, int noteId) {
        Session session = this.sessionFactory.openSession();
        try {
            session.beginTransaction();
            GroupNotes notes = (GroupNotes) session.get(GroupNotes.class, noteId);
            if(notes!=null) {
                notes.getGroupNoteHistorySet().add(noteHistory);
                noteHistory.setMhistory(notes);
                 session.save(noteHistory);
                session.flush();
                session.getTransaction().commit();
                return noteHistory.getMhistoryid();
            }
        } catch (Exception e) {
          e.printStackTrace();
        }finally {
            session.close();
        }
        return 0;
    }