2016-10-25 6 views
0

最近私たちは非常に奇妙な問題が発生しているHibernateとPostgreSQLのSpring MVCアプリケーションに取り組んでいます。ときどきオブジェクトが永続化されているときに、私はそれをDAOレイヤーからサービスレイヤーに戻しています。このIDはPUSHフレームワークを介してブロードキャストされ、その後オブジェクトは呼び出しによって取得されます。この時点では、オブジェクトが保存されているにもかかわらず、NPEが取得されています。プライマリキーのIDはゼロではなく、セッションはIDを返す前にフラッシュされます。何がうまくいかないでしょうか?Spring、Hibernate:オブジェクトを保存するためのデータベースコールが完了しましたが、null値を返すことがあります

例:

@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) 
+0

は、データベース内のエンティティですか? –

+0

@PeterGelderbloem:それは問題です、私はIDを取得しているので、そこにいるはずですが、それはうまくいかないようです。 –

+0

@WeareBorg例外とGroupNoteHistoryエンティティを投稿できますか?どのラインでNPEが投げられるのですか? – Atul

答えて

1

int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory, noteId);あなたはすでにGroupeNoteHistoryを持っています。

この後、あなたはそれを操作してGroupNoteHistoryがすでに存在し、それを使用する前に、nullでない場合、それは

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

だから、あなたは確認する必要があります保存します。そういうわけで、あなたはNPEを手に入れます。

+0

あなたが 'int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory、noteId);'の引数のGroupeNoteHistoryがヌルである理由を調査する必要があります – Ruddy

+0

実際には、オブジェクトが保存されています。 DAOメソッドから、しかし後で呼び出されるときに、私たちはnullオブジェクトを取得しています。ときどき...奇妙な行動...これは私たちが検査しようとしていることです。何か案は? –

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; 
    } 
+1

あなたはすべての例外を捕まえるべきではありません...悪い習慣です。無効をテストし、エラーメッセージを記録する必要があります。そして、ヌルでない場合にのみ 'noteHistory'オブジェクトを確実に操作してください;-) – Ruddy

+0

' noteHistory'がnullの場合、 'IllegalArgumentException'を投げることができます。 – Ruddy

+0

@Ruddy:問題は、ユーザーがいったんコメントを追加すると、それが何であっても持続していることを保証する必要があるということです。修正するためにできるだけ早く起こっているエラーを知る必要があります...また、これはテストインスタンス、ライブインスタンスの場合、私たちの監視ツールにプッシュしています。:-) –

関連する問題