Query.list()中的Hibernate NullPointerException

时间:2013-08-06 07:12:03

标签: java hibernate

仅使用以下stacktrace

再现一次
  

引起:java.lang.NullPointerException         在org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:119)         at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)         在org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)         at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:970)         在org.hibernate.impl.SessionImpl.list(SessionImpl.java:1115)         在org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)

任何人都知道可能是什么原因?

Hibernate版本是3.2.7.GA

我确信原因不在当前查询中,因为每个列表都执行。为什么可以刷新当前会话?它可以以某种方式被阻止吗?

1 个答案:

答案 0 :(得分:1)

以下是生成nullpointer的方法(来源:http://grepcode.com/file_/repo1.maven.org/maven2/org.hibernate/hibernate/3.2.6.ga/org/hibernate/event/def/AbstractFlushingEventListener.java/?v=source):

private void prepareEntityFlushes(EventSource session) throws HibernateException {

    log.debug("processing flush-time cascades");

    final Map.Entry[] list = IdentityMap.concurrentEntries( session.getPersistenceContext().getEntityEntries() );
    //safe from concurrent modification because of how entryList() is implemented on IdentityMap
    final int size = list.length;
    final Object anything = getAnything();
    for ( int i=0; i<size; i++ ) {
        Map.Entry me = list[i];
        EntityEntry entry = (EntityEntry) me.getValue(); //nullpointer here!
        Status status = entry.getStatus();
        if ( status == Status.MANAGED || status == Status.SAVING ) {
            cascadeOnFlush( session, entry.getPersister(), me.getKey(), anything );
        }
    }
}

IdentityMap.concurrentEntries返回一个Map.Entry数组,其中包含所提供地图的条目。看起来在地图中从session.getPersistenceContext().getEntityEntries()返回了一个null元素,尝试获取源代码(应该可以通过大多数IDE)并调试该方法。另外,通过你的实体定义,可能有些不对劲..