I am working on a Spring-MVC appplication with Hibernate and PostgreSQL in which these days we are experiencing a very strange problem. Sometimes when an object is being persisted, I am returning it's ID from the DAO layer to the service layer. This ID is then being broadcasted via our PUSH framework, after which point the object is retrieved by a call. At this point I am getting a NPE, although the object is saved, primary-key ID is non-zero, session is also flushed before returning the ID. What might be going wrong?
Example :
@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 method :
@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;
}
}
}
Now, after broadcasting the ID, this method is called :
@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 method :
@Override
public GroupNoteHistory getGroupNoteHistoryById(int historyId) {
Session session = this.sessionFactory.getCurrentSession();
return (GroupNoteHistory) session.get(GroupNoteHistory.class, historyId);
}
Any thoughts?
Update
Error log :
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)